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?
2 Answers
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.
2 Comments
Max
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.
vrish88
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.