Reputation: 383
I'm actually having trouble finding the documentation for this so if you have a link handy that would be really appreciated too.
So I have:
resources :users do
resources :posts, only: [:index, :create, :show]
end
I wanted to access the index action of posts
through a named route. I tried this: <%= link_to 'User Posts', user_posts_path %>
but it said it was missing user_id
. Any ideas?
Upvotes: 0
Views: 202
Reputation: 5623
When using the nested resource routes, you would need to provide the reference id of the parent resource. In your case resource user
. You could do: user_posts_path(user)
. The route generated would be something like: /users/1/posts
where 1 is the :user_id
or if you would rather want a route like: /users/posts
you should do:
resources :users do
collection do
resources :posts
end
end
Find full routing documentation here
Upvotes: 2
Reputation: 1176
it's asking for user_id
because you're defining :users as a resource, change it for a namespace instead:
namespace :users do
resources :posts, only: [:index, :create, :show]
end
Upvotes: 0