views:

234

answers:

1

I have a namespaced controller for some admin functionality. My create form does not work -- it ends up routing the request to the index action instead of the create action.

Why isn't the POST getting routed to the create action as it should (being RESTful)?

routes.rb:

  map.namespace :admin do |admin|
    admin.resources :events
  end

rake routes:

             admin_events GET    /admin/events                   {:action=>"index", :controller=>"admin/events"}
    formatted_admin_events GET    /admin/events.:format           {:action=>"index", :controller=>"admin/events"}
                               POST   /admin/events                   {:action=>"create", :controller=>"admin/events"}
                               POST   /admin/events.:format           {:action=>"create", :controller=>"admin/events"}
           new_admin_event GET    /admin/events/new               {:action=>"new", :controller=>"admin/events"}
 formatted_new_admin_event GET    /admin/events/new.:format       {:action=>"new", :controller=>"admin/events"}
          edit_admin_event GET    /admin/events/:id/edit          {:action=>"edit", :controller=>"admin/events"}
formatted_edit_admin_event GET    /admin/events/:id/edit.:format  {:action=>"edit", :controller=>"admin/events"}
               admin_event GET    /admin/events/:id               {:action=>"show", :controller=>"admin/events"}
     formatted_admin_event GET    /admin/events/:id.:format       {:action=>"show", :controller=>"admin/events"}
                               PUT    /admin/events/:id               {:action=>"update", :controller=>"admin/events"}
                               PUT    /admin/events/:id.:format       {:action=>"update", :controller=>"admin/events"}
                               DELETE /admin/events/:id               {:action=>"destroy", :controller=>"admin/events"}
                               DELETE /admin/events/:id.:format       {:action=>"destroy", :controller=>"admin/events"}

app/views/admin/events/new.html.erb:

<h1>New event</h1>

<% form_for([:admin, @event]) do |f| %>
  <%= f.error_messages %>
  ...

app/controllers/admin/event_controller.rb:

class Admin::EventsController < ApplicationController
  def index
    @events = Event.find(:all)
    ...
  end

  def create
    @event = Event.new(params[:event])
    ...
  end

  ...
end

And finally, a bit of a log file where you can see it is indeed POSTing:

Processing Admin::EventsController#index (for 127.0.0.1 at 2008-10-16 18:12:47) [POST]
  Session ID: ...
  Parameters: {"commit"=>"Create", "authenticity_token"=>"...", "action"=>"index", "controller"=>"admin/events", "event"=>{"location"=>""}}
  Event Load (0.000273)   SELECT * FROM `events` 
Rendering template within layouts/application
Rendering admin/events/index
Completed in 0.00757 (132 reqs/sec) | Rendering: 0.00118 (15%) | DB: 0.00027 (3%) | 200 OK [http://localhost/admin/events]
+2  A: 

The order of the routes was the issue. I'm not sure exactly why, but moving it below my root routes (map.connect '') solves the issue, and Rails routes the requests accordingly.

Ian Terrell
Thanks a lot. This unexplainable bug just about to blow up my brain! Thanks a lot for this question.
Antiarchitect
Just want to ask what's the difference between `map.connect ''` and `map.root`?
PeterWong