Rails 3 link_to 路由(编辑)嵌套资源 [英] Rails 3 link_to routes (edit) nested resources

查看:36
本文介绍了Rails 3 link_to 路由(编辑)嵌套资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉,如果在其他地方有人问过这个问题,但我无法弄清楚.我有一个包含部分、主题和回复的论坛.我正在尝试编辑和删除节目主题视图中的回复.这是结构:

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

所以我做了一个 rake 路由,看看我在哪里链接我的编辑回复.我看到它的 edit_section_topic_reply 并在我的 link_to 中添加了 _path .现在这是我想不通的.我传递了什么参数?不应该是:

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) %>

当我这样做时,我在 Topics#show 中得到一个 ActionController::RoutingError.

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">}

好像是不传id,不过嵌套之前,我的新话题没问题

It seems like it isn't passing IDs, but the nest before, my new topic works fine

new_section_topic_reply_path(@topic, @section)

推荐答案

我真的很不喜欢 link_to 助手的这一方面.为了让您的代码更易读且不易出错,我建议您明确说明您要传入的 ID.

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.

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

由于 link_to 中的参数乱序,我遇到了太多微妙且看似疯狂的错误.

I've run into too many subtle and seemingly insane bugs due to params being out of order in a link_to.

这篇关于Rails 3 link_to 路由(编辑)嵌套资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆