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

发布于 2024-10-20 03:44:53 字数 1057 浏览 2 评论 0原文

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

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)

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

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

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

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

new_section_topic_reply_path(@topic, @section)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝天 2024-10-27 03:44:53

我真的不喜欢 link_to 帮助器的这一方面。为了使您的代码更具可读性且不易出错,我建议您明确要传入的 ID。

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

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

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

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

追我者格杀勿论 2024-10-27 03:44:53

编辑链接的另一种方法

<%= link_to [:edit,@section,@topic,@reply] %>

One more way for edit link

<%= link_to [:edit,@section,@topic,@reply] %>
十年九夏 2024-10-27 03:44:53

我认为正确的顺序应该是:

<%= link_to 'Edit', edit_section_topic_reply_path(@section, @topic, @reply) %>

I think the right order should be:

<%= link_to 'Edit', edit_section_topic_reply_path(@section, @topic, @reply) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文