使用命名空间路由时出现路由错误

发布于 2024-11-10 08:21:53 字数 926 浏览 1 评论 0原文

当路由被命名时,你如何处理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 技术交流群。

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

发布评论

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

评论(2

雨落星ぅ辰 2024-11-17 08:21:53

尝试使用 simple_form_for([:admin, @company]) do |f|

Try using simple_form_for([:admin, @company]) do |f|

人间不值得 2024-11-17 08:21:53

我相信我只需要在路径末尾将路径复数,如下所示:

<%= simple_form_for(@company, :url => admin_companies_path(@company)) do |f| %>

这不是我所期望的。我只是猜测而已。这不是有效的路线或任何东西,但它似乎适用于 put 和 posts。

I believe I just have to pluralize the path at the end of the path, like this:

<%= simple_form_for(@company, :url => admin_companies_path(@company)) do |f| %>

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.

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