Rails 3 link_to路由(编辑)嵌套资源
对不起,如果有人问其他地方,但我无法弄清楚这一点。 我有一个论坛,其中包含部分,主题和回复。 我正在尝试编辑和删除show topic视图中的回复。 这是结构:
resources :sections do resources :topics do resources :replies end end
所以我做了一个rake路线,看看我在哪里链接我的编辑回复。 我看到它的edit_section_topic_reply和我的link_to中我添加了_path。 现在这是我无法弄清楚的。 我通过什么参数? 不应该是:
当我这样做时,我在Topics#show
得到一个ActionController::RoutingError
。
No route matches {:topic_id=>#, :controller=>"replies", :action=>"edit", :section_id=>nil, :id=>#}
好像它不是传递ID,而是之前的巢,我的新主题运行正常
new_section_topic_reply_path(@topic, @section)
我真的不喜欢link_to
帮助器的这个方面。 为了使您的代码更具可读性并且不易出错,我建议您明确说明您传入的ID。
<%= link_to 'Edit', edit_section_topic_reply_path(:id => @reply.id, :topic_id => @topic.id, :section_id => @section.id) %>
由于params在一个link_to
出现故障,我遇到了太多微妙且看似疯狂的错误。
编辑链接的另一种方法
<%= link_to [:edit,@section,@topic,@reply] %>
我认为正确的顺序应该是:
<%= link_to 'Edit', edit_section_topic_reply_path(@section, @topic, @reply) %>