简体   繁体   English

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

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

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. 我正在尝试编辑和删除show topic视图中的回复。 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. 所以我做了一个rake路线,看看我在哪里链接我的编辑回复。 I see that its edit_section_topic_reply and in my link_to I add _path to it. 我看到它的edit_section_topic_reply和我的link_to中我添加了_path。 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. 当我这样做时,我在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">}

It seems like it isn't passing IDs, but the nest before, my new topic works fine 好像它不是传递ID,而是之前的巢,我的新主题运行正常

new_section_topic_reply_path(@topic, @section)

I really dislike this aspect of the link_to helper. 我真的不喜欢link_to帮助器的这个方面。 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. 为了使您的代码更具可读性并且不易出错,我建议您明确说明您传入的ID。

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

I've run into too many subtle and seemingly insane bugs due to params being out of order in a link_to . 由于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