Tutorial :Ruby on Rails: New controller for photos?



Question:

I have a one-to-many relationship between my Profile class and Photos class. Profile holds the user's information and photos holds the user's pictures. Currently, the "Upload photo" link shows at the bottom of the profile view. I want to have the link show up on a separate view instead of cramming everything on the same form. Should I just create a new view called profile_photo.html.erb to show this link or should I create a new controller and model that associates with the photos? What's the recommended practice?


Solution:1

The "Photos" class should be called "Photo", as Rails uses singular names for models.

Yes you should create a new controller for creating photos because it's another resource. You'll probably want to edit the photos eventually, adding descriptions and so on.


Solution:2

I agree with Radar, you'll want to create a PhotosController. When you do that then you can take it a step further and define the has_many relationship in the routes.rb file as well. Like so:

   map.resources :photos     map.resources :profiles, :has_many => :photos  

This will generate urls like profile_photos_path, new_profile_photo_path, etc... run the rake routes command to see what it gives you. This will help to keep you code DRY and easy to read.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »