Sam 山
Sam 山

Reputation: 42865

Rails: what are the benefits of using nested routes / forms

I am starting to use some nested routes which is definitely a different way of planning things and I'm really failing to see added benefits.

So what are they?

Upvotes: 1

Views: 630

Answers (1)

concept47
concept47

Reputation: 31726

It allows you stay Restful and automatically route urls where more than one model is involved.

So if you have a user model that has many posts, you can nest the posts model in the user model and make a call like this

<%=link_to "Users posts", user_posts_path(@user) %>

which would route automagically to the index action of the Posts Controller via

/users/4/posts

passing the user id as a parameter that you can use to display all the posts just for that user.

A good link to get more info http://guides.rubyonrails.org/routing.html#nested-resources

Upvotes: 1

Related Questions