【问题标题】:Routing Rails Admin Controller Create Action路由 Rails 管理控制器创建操作
【发布时间】:2011-09-25 12:56:07
【问题描述】:

除创建记录外,所有操作(索引、...)都正确使用了专用 admin/countries_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 help with building Admin area - Routing problem

【问题讨论】:

  • 在您看来,您是如何构建发起 POST 请求的 url 的?
  • <%= link_to 'New Country', new_admin_country_path %> 导致标准的脚手架表单助手,包含<%= form_for(@country) do |f| %><%= f.submit %>

标签: ruby-on-rails controller routes admin


【解决方案1】:

您的form_for 也需要命名空间:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 2011-12-11
    相关资源
    最近更新 更多