Rails form_for 与嵌套资源:“没有路由匹配”

发布于 2024-12-14 12:38:58 字数 983 浏览 3 评论 0原文

我的 routes.rb 文件中有以下嵌套资源。内部资源指定控制器名称。

resources :batches, :except => [:new], :path => "sets" do
  resources :tags, :controller => "batches_tags"
end

BatchesTags#new 的视图中,我尝试构建一个表单:

<%= form_for [@batch, @tag], :url => batch_tag_path do |f| %>
  ...
<% end %>

尝试加载此页面 (/sets/1/tags/new) 为我提供了一个 ActionController: :路由错误,消息如下:

没有路线匹配 {:action=>"show", :controller=>"batches_tags"}

但是当我运行 $ rake paths 时,它清楚地显示了这条路线确实存在:

batch_tag GET    /sets/:batch_id/tags/:id(.:format)        {:action=>"show", :controller=>"batches_tags"}

有谁知道如何修复这个错误?

编辑:

Batches#show视图中,我使用相同的batch_tag_path函数并且它工作得很好:

<%= link_to "...", batch_tag_path(@batch, tag) %>

I have the following nested resources in my routes.rb file. The inner resource specifies a controller name.

resources :batches, :except => [:new], :path => "sets" do
  resources :tags, :controller => "batches_tags"
end

In the view for BatchesTags#new, I am trying to build a form:

<%= form_for [@batch, @tag], :url => batch_tag_path do |f| %>
  ...
<% end %>

Attempting to load this page (/sets/1/tags/new) gives me a ActionController::RoutingError with the message:

No route matches {:action=>"show", :controller=>"batches_tags"}

But when I run $ rake routes, it clearly shows this route does exist:

batch_tag GET    /sets/:batch_id/tags/:id(.:format)        {:action=>"show", :controller=>"batches_tags"}

Does anyone know how to fix this error?

Edit:

In the view for Batches#show, I use that same batch_tag_path function and it works perfectly:

<%= link_to "...", batch_tag_path(@batch, tag) %>

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

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

发布评论

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

评论(1

梦冥 2024-12-21 12:38:58

事实证明,虽然 batch_tag_path 是一条有效的路由(使得“无路由匹配”错误消息非常混乱),但我需要的路径是复数的 batch_tags_path,如 $ rake paths 输出所示:

batch_tags GET    /sets/:batch_id/tags(.:format)   {:action=>"index", :controller=>"batches_tags"}
           POST   /sets/:batch_id/tags(.:format)   {:action=>"create", :controller=>"batches_tags"}

也许错误消息意味着 batch_tag_path 不是 POST 的有效路由?

It turns out that, while batch_tag_path is a valid route (making the "No route matches" error message very confusing), the path I needed was the pluralized batch_tags_path, as seen in this $ rake routes output:

batch_tags GET    /sets/:batch_id/tags(.:format)   {:action=>"index", :controller=>"batches_tags"}
           POST   /sets/:batch_id/tags(.:format)   {:action=>"create", :controller=>"batches_tags"}

Perhaps the error message meant that batch_tag_path wasn't a valid route for POST?

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