2013-01-12 61 views
-1

我有一個路由錯誤,但我認爲我的路線是正確的:Rails的路由錯誤,但是路線正確顯示

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. 

當我執行rake routes,結果是正確的:

 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 

我路線是:

​​

,並在錯誤的鏈接是:

<%= link_to 'Edit', edit_user_post_path(notice) %> 

我不知道什麼是錯的。

回答

2

既然你要編輯屬於一個特定用戶一個特定後,你需要在這兩個作爲參數傳遞給該鏈接通過,把父母第一。否則,rails不知道你要編輯哪個文章。

<%= link_to 'Edit', edit_user_post_path(@user, @post) %> 

所以你似乎需要的是這兩個變量。

仔細閱讀你的耙路。它說:

/user/:user_id/posts/:id/edit(.:format) 

所以你可以看到,你需要:user_id以及:id,它指的是崗位。將對象作爲參數就足夠了,rails非常聰明,能夠找出它們的id並將它們用於鏈接。

+0

不工作,我通過用戶,但有這個錯誤:PostsController中的AbstractController :: DoubleRenderError#編輯 渲染和/或重定向在此操作中被多次調用。請注意,您只能調用渲染或重定向,並且每次最多隻能調用一次。另外請注意,重定向和呈現都不會終止該動作的執行,因此如果您想在重定向後退出動作,則需要執行諸如「redirect_to(...)並返回」之類的操作。 – overallduka

+0

錯誤消息解釋了這一切。看看你的postscontroller的編輯方法 - 看起來你正在渲染/重定向到兩次。擺脫一個例子。 – weltschmerz

+0

所以,現在它的問題在您的控制器操作不在路由 –