Routing Rails 管理控制器创建操作

发布于 2024-11-18 01:17:31 字数 1310 浏览 2 评论 0原文

除创建记录外,专用的 admin/countries_controller 已正确用于所有操作(索引等)。这里父控制器目录中的常规country_controller处于活动状态:

Started POST "/countries" for 127.0.0.1 at 2011-06-29 23:26:38 +0200
  Processing by CountriesController#create as HTML

将POST操作路由到admin/countries缺少什么?

routes.rb:

  resources :countries

  namespace :admin do
    resources :countries
  end

rake 路线:

     countries GET    /countries(.:format)                {:action=>"index", :controller=>"countries"}
               POST   /countries(.:format)                {:action=>"create", :controller=>"countries"}
   new_country GET    /countries/new(.:format)            {:action=>"new", :controller=>"countries"}

   admin_countries GET    /admin/countries(.:format)          {:action=>"index", :controller=>"admin/countries"}
                   POST   /admin/countries(.:format)          {:action=>"create", :controller=>"admin/countries"}
 new_admin_country GET    /admin/countries/new(.:format)      {:action=>"new", :controller=>"admin/countries"}

此处未回答类似问题: Rails 帮助构建管理区域 - 路由问题

A dedicated admin/countries_controller is being correctly used for all actions (index, ...), except for creating records. Here the regular countries_controller from the parent controller directory is active:

Started POST "/countries" for 127.0.0.1 at 2011-06-29 23:26:38 +0200
  Processing by CountriesController#create as HTML

What is missing to have the POST action being routed to admin/countries?

routes.rb:

  resources :countries

  namespace :admin do
    resources :countries
  end

rake routes:

     countries GET    /countries(.:format)                {:action=>"index", :controller=>"countries"}
               POST   /countries(.:format)                {:action=>"create", :controller=>"countries"}
   new_country GET    /countries/new(.:format)            {:action=>"new", :controller=>"countries"}

   admin_countries GET    /admin/countries(.:format)          {:action=>"index", :controller=>"admin/countries"}
                   POST   /admin/countries(.:format)          {:action=>"create", :controller=>"admin/countries"}
 new_admin_country GET    /admin/countries/new(.:format)      {:action=>"new", :controller=>"admin/countries"}

Similar question unanswered here:
Rails help with building Admin area - Routing problem

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

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

发布评论

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

评论(1

我的鱼塘能养鲲 2024-11-25 01:17:31

您的 form_for 也需要命名空间:

<%= form_for [:admin, @country] do |f| %>
   ...
<% end %>

当您将 @country 传递给 form_for 时,它不会知道您希望此表单进入哪个命名空间因此它将默认为标准的 POST /countries URL。

Your form_for needs to be namespaced too:

<%= form_for [:admin, @country] do |f| %>
   ...
<% end %>

When you pass @country to form_for it's not going to know what namespace you want this form to go to and so it will default to just the standard POST /countries URL.

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