4

I have this structure in my app:

USER has_one :publicprofile, :privateprofile

PUBLICPROFILE has many :emails, :phonenumbers

PRIVATEPROFILE has many :adresses, :creditcards

I would like to know how to go about having a profile page for the user where I can update his nested resources (and do it in a RESTful way). I couldn't find any docs/examples on the subject (because of that confusing has_one relation).

2
  • What exactly would you like to know? How to go about routes? Commented Dec 18, 2008 at 22:51
  • How to create a simple crud/scaffold for that situation and how to write the routes. Also if you have one profile page, how do you go about putting the pieces together ? Do you render each model's views as snippets?Or do you create other forms for each resource?I think an example would clear things Commented Dec 18, 2008 at 23:14

3 Answers 3

4

I believe any nesting deeper than 1-level is generally frowned upon, and can normally be avoided. Jamis Buck blogged about it a while back.

The PeepCode REST for Rails 2 screencast is quite good too, but it doesn't go extensively into nesting, just resources in general.

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

Comments

4

In your routes.rb file, you can add your profiles as nested resources for the users using a block:

map.resources :users do |user|
  user.resources :privateprofile
  user.resources :publicprofile
end

Then you can access your profile using a URL something like this:

users/1/publicprofiles
users/1/publicprofiles/new
users/1/publicprofiles/1/edit

You can run rake routes in your terminal to get a listing of all of the nested urls that are available to you after update your routes file.

For a really in-depth explanation see this post: http://adam.blog.heroku.com/past/2007/12/20/nested_resources_in_rails_2/

Comments

2

I recommend you watch this screencast: http://railscasts.com/episodes/139-nested-resources

It definitely helped me understand what is really possible using nested routes and resources in Rails (especially the new changes in Rails 2.2).

However, I'll have to agree with Ryan Bates here, use nested resources with care.

Comments

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.