嵌套路由 link_to 助手在一个地方工作而不是另一个地方 [英] Nested Route link_to helper working in one place but not another
问题描述
我有一个无法弄清楚的嵌套路由问题.我有一个具有嵌套路由的应用,如下所示:
I have a nested routes problem that I can not figure out. I have an app that has nested routes like so:
resources :events do
resources :sessions
end
我正在尝试在我的代码中使用以下 link_to:
I am trying to use the following link_to in my code:
<%= link_to "New Session", new_event_session_path %>
当我运行 rake 路由时,它会显示正确的 URL 并且存在 GET 方法:
When I run rake routes, it will show the proper URL with a GET method existing:
new_event_session GET /events/:event_id/sessions/new(.:format) {:action=>"new", :controller=>"sessions"}
当我尝试在一个地方使用 link_to 时它可以工作,当我尝试在另一个地方使用它时它不起作用,它反而会给我这个错误:
When I try to use the link_to in one place it works, when I try to use it in another place it does not, it instead will give me this error:
No route matches {:controller=>"sessions", :action=>"new"}
这两个文件的唯一区别是文件在应用程序中的位置(一个在视图/事件下,另一个在视图/会话和被调用的网址下:
The only difference between the two files is the location of the files in the app (one is under views/events the other is under views/sessions and the url being called:
/events/1 --vs--/events/1/sessions
/events/1 --vs-- /events/1/sessions
我仍然是一个有导轨的菜鸟,所以这可能是一个愚蠢的问题,但我遇到了一些障碍.任何帮助表示赞赏.
I am still a noob with rails, so this is probably a stupid question, but I have hit a bit of a wall. Any help is appreciated.
推荐答案
你只需要将事件对象传递给路径助手:
You just need to pass event object to path helper:
new_event_session_path(@event)
这篇关于嵌套路由 link_to 助手在一个地方工作而不是另一个地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!