开发者

Routing Rails Admin Controller Create Action

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   /count开发者_如何学编程ries(.: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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜