Lai Yu-Hsuan
Lai Yu-Hsuan

Reputation: 28071

how should rails nested resources path look?

I have a resource nested in another:

resource :users do
  resource :posts
end

The official guide says I should have this url:

/users/:id/posts/:id

To show update edit or delete the posts. But in fact I have:

ruby-1.9.2-p290 > app.users_posts_path(1,2)
=> "/users/posts.1?=2" 

What's going wrong?

Upvotes: 1

Views: 717

Answers (2)

Paulo Abreu
Paulo Abreu

Reputation: 1786

Did you try to see your routes? in you console type:

$ bundle exec rake routes

Usually user_posts_path is the GET request for listing items (are you sure that in your route name user is plural?), so you just need to provide the first parameter that represents your :id, related to users.

Upvotes: 0

Dylan Markow
Dylan Markow

Reputation: 124419

You need to be using standard plural routes, not singular, so use resources instead of resource:

resources :users do
  resources :posts
end

Upvotes: 2

Related Questions