使用命名空间路由时出现路由错误
当路由被命名时,你如何处理form_for
?我收到了一些我真正期望得到的奇怪的路线错误。
例如,假设您有一个名为 Admin::CompaniesController
的控制器,位于 你的routes.rb中的 :admin
命名空间:
namespace :admin do
resources :companies
end
大多数事情都工作正常,但是当我渲染新表单时,我收到错误。这是代码:
<%= simple_form_for(@company, :url => admin_company_path(@company)) do |f| %>
这是错误消息:
ActionView::Template::Error: No route matches {:action=>"show", :controller=>"admin/companies", :id=>#<Company id: nil, name: nil, phone_number: nil, address: nil, postal_code: nil, is_enabled: true, courses_created: 0, province_id: nil, theme_id: nil, payment_plan_id: nil, created_at: nil, updated_at: nil>}
如何让 Rails 在这里发挥良好作用?我显然想要一个用于编辑的网址,另一个用于新表单的网址。通常,我什至不需要将 :url
放入 form_for
语句中,但由于嵌套,我被迫这样做。
我现在不知道在这里做什么,至少不知道该怎么做。
How do you deal with form_for
's when the routes are namespaced? I am getting some weird route errors that I really expect to get.
For example, let's say you have a controller called Admin::CompaniesController
in
your :admin
namespace in your routes.rb:
namespace :admin do
resources :companies
end
Most things work just fine, but I get an error when I render a new form. Here's the code:
<%= simple_form_for(@company, :url => admin_company_path(@company)) do |f| %>
And here's the error message:
ActionView::Template::Error: No route matches {:action=>"show", :controller=>"admin/companies", :id=>#<Company id: nil, name: nil, phone_number: nil, address: nil, postal_code: nil, is_enabled: true, courses_created: 0, province_id: nil, theme_id: nil, payment_plan_id: nil, created_at: nil, updated_at: nil>}
How can I get rails to play nice here? I obviously want one url for edits, and another for new forms. Usually, I'd never even have to put :url
in my form_for
statements, but because of the nesting, I am forced to.
I have no idea what to do here now, at least not elegantly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 simple_form_for([:admin, @company]) do |f|
Try using
simple_form_for([:admin, @company]) do |f|
我相信我只需要在路径末尾将路径复数,如下所示:
这不是我所期望的。我只是猜测而已。这不是有效的路线或任何东西,但它似乎适用于 put 和 posts。
I believe I just have to pluralize the path at the end of the path, like this:
This is not what I would have expected. I just guessed at it. This is not a valid route or anything, but it seems to work for puts and posts.