David
David

Reputation: 957

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   /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

Upvotes: 0

Views: 1020

Answers (1)

Ryan Bigg
Ryan Bigg

Reputation: 107728

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.

Upvotes: 1

Related Questions