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?
+3
A:
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.
Ryan Bigg
2009-03-23 05:16:04
+3
A:
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.
vrish88
2009-03-23 05:22:42
When a new user completes the profiles form, he is redirected to the photos form but how do I get profile_id inserted in the photos table? Should I pass the profile_id when doing a redirect_to in the profiles controller? In the photos controller, I can manually insert the passed profile_id.
Max
2009-04-05 03:02:42
Once the user completes a profiles form are you directing them to photos_path or profile_photos_path? If you are using the later then the profile_id should be included in the url and then you can access it through the params hash.
vrish88
2009-04-06 17:25:37