I have a routing error, but I think my routes are correct:
Routing Error
No route matches {:controller=>"posts", :action=>"edit", :user_id=>#<Post id: 9, title: "Na Curva do Horizonte", content: "Eu na mesma minha opinião no pensamento vejo me ca...", created_at: "2013-01-12 20:41:57", updated_at: "2013-01-12 20:41:57", image_file_name: "iris_by_archang3lzz-d5k2i5l.jpg", image_content_type: "image/jpeg", image_file_size: 1101282, image_updated_at: "2013-01-12 20:41:56", user_id: 5>}
Try running rake routes for more information on available routes.
When I execute rake routes, the results are correct:
user_posts GET /user/:user_id/posts(.:format) posts#index
POST /user/:user_id/posts(.:format) posts#create
new_user_post GET /user/:user_id/posts/new(.:format) posts#new
edit_user_post GET /user/:user_id/posts/:id/edit(.:format) posts#edit
user_post GET /user/:user_id/posts/:id(.:format) posts#show
My routes are:
resources :posts
resources :user do
resources :posts,:comments
end
resources :posts do
resources :comments
end
and the link in error is:
<%= link_to 'Edit', edit_user_post_path(notice) %>
I don’t know what is wrong.
Since you want to edit a specific post which belongs to a specific user, you need to pass in those two as parameters to that link, putting the parent first. Otherwise rails doesn’t know which post you are about to edit.
So all you seem to need is those two variables.
Read you rake routes carefully. It says:
So there you can see that you need
:user_idas well as:id, which refers to the post. Giving the objects as parameters will be enough, rails is smart enough to figure out their ids and use them for the link.