Rails 3 link_to 路由(编辑)嵌套资源
抱歉,如果其他地方有人问过这个问题,但我无法弄清楚。我有一个包含版块、主题和回复的论坛。我正在尝试从显示主题视图中编辑和删除回复。这是结构:
resources :sections do
resources :topics do
resources :replies
end
end
所以我做了一个 rake 路线来查看我在哪里链接我的编辑回复。我看到它的 edit_section_topic_reply 并在我的 link_to 中添加了 _path 。现在这是我无法弄清楚的。我传递什么参数?不应该是这样吗:
<%= link_to 'Edit', edit_section_topic_reply_path(@reply, @topic, @section) %>
当我这样做时,我在 Topics#show
中收到 ActionController::RoutingError
。
No route matches {:topic_id=>#<Topic id: 2, section_id: 2, user_id: nil, subject: "subject", body: "body", created_at: "2011-03-04 08:37:37", updated_at: "2011-03-04 21:37:16">, :controller=>"replies", :action=>"edit", :section_id=>nil, :id=>#<Section id: 2, name: "Section", description: "Section Description", created_at: "2011-03-04 07:50:56", updated_at: "2011-03-04 07:50:56">}
看起来好像没有传递ID,但是之前的嵌套,我的新主题工作正常
new_section_topic_reply_path(@topic, @section)
Sorry if this has been asked elsewhere, but I can't figure this out. I have a forum with sections, topics, and replies. I'm trying to edit and delete replies from the show topic view. This is the structure:
resources :sections do
resources :topics do
resources :replies
end
end
So I do a rake routes to see where I'm linking my edit reply. I see that its edit_section_topic_reply and in my link_to I add _path to it. Now this is what I can't figure out. What parameters am I passing it? Shouldn't it be:
<%= link_to 'Edit', edit_section_topic_reply_path(@reply, @topic, @section) %>
I get a ActionController::RoutingError
in Topics#show
when I do this.
No route matches {:topic_id=>#<Topic id: 2, section_id: 2, user_id: nil, subject: "subject", body: "body", created_at: "2011-03-04 08:37:37", updated_at: "2011-03-04 21:37:16">, :controller=>"replies", :action=>"edit", :section_id=>nil, :id=>#<Section id: 2, name: "Section", description: "Section Description", created_at: "2011-03-04 07:50:56", updated_at: "2011-03-04 07:50:56">}
It seems like it isn't passing IDs, but the nest before, my new topic works fine
new_section_topic_reply_path(@topic, @section)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我真的不喜欢
link_to
帮助器的这一方面。为了使您的代码更具可读性且不易出错,我建议您明确要传入的 ID。由于参数乱序,我遇到了太多微妙且看似疯狂的错误 。一个
link_to
。I really dislike this aspect of the
link_to
helper. In the interest of making your code more readable and less prone to error, I would suggest that you be explicit about which IDs you are passing in.I've run into too many subtle and seemingly insane bugs due to params being out of order in a
link_to
.编辑链接的另一种方法
One more way for edit link
我认为正确的顺序应该是:
I think the right order should be: