繁体   English   中英

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

[英]Rails 3 link_to routes (edit) nested resources

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

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)

我真的不喜欢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) %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM