Rails 3 link_to路由(编辑)嵌套资源

a3u*_*uge 8 ruby-on-rails nested-routes ruby-on-rails-3

对不起,如果有人在其他地方询问,但我无法弄清楚这一点.我有一个论坛,其中包含部分,主题和回复.我正在尝试编辑和删除show topic视图中的回复.这是结构:

resources :sections do
  resources :topics do
    resources :replies
  end
end
Run Code Online (Sandbox Code Playgroud)

所以我做了一个rake路线,看看我在哪里链接我的编辑回复.我看到它的edit_section_topic_reply和我的link_to中我添加了_path.现在这是我无法弄清楚的.我通过什么参数?不应该是:

<%= link_to 'Edit', edit_section_topic_reply_path(@reply, @topic, @section) %>
Run Code Online (Sandbox Code Playgroud)

我得到一个ActionController::RoutingErrorTopics#show我这样做的时候.

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">}
Run Code Online (Sandbox Code Playgroud)

好像它不是传递ID,而是之前的巢,我的新主题运行正常

new_section_topic_reply_path(@topic, @section)
Run Code Online (Sandbox Code Playgroud)

jdl*_*jdl 14

我真的不喜欢这个link_to帮手的方面.为了使您的代码更具可读性并且不易出错,我建议您明确说明您传入的ID.

<%= link_to 'Edit', edit_section_topic_reply_path(:id => @reply.id, 
                                                  :topic_id => @topic.id, 
                                                  :section_id => @section.id) %>
Run Code Online (Sandbox Code Playgroud)

我遇到了太多微妙而看似疯狂的错误,因为params在一个故障中失灵了link_to.