2

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 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.