Rails form_for 与嵌套资源:“没有路由匹配”
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,虽然
batch_tag_path
是一条有效的路由(使得“无路由匹配”错误消息非常混乱),但我需要的路径是复数的batch_tags_path
,如$ rake paths
输出所示:也许错误消息意味着
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 pluralizedbatch_tags_path
, as seen in this$ rake routes
output:Perhaps the error message meant that
batch_tag_path
wasn't a valid route for POST?