2
votes

I followed below tutorial and built one whole complete app.

http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/ http://www.tonyamoyal.com/2010/09/29/rails-authentication-with-devise-and-cancan-part-2-restful-resources-for-administrators/comment-page-2/#comment-879

All is well until I ran into a problem towards the end.

The problem is that when a super_admin user try to create a new user. I got error:

Started POST "/users" for 127.0.0.1 at 2012-03-09 23:37:51 -0500
  Processing by RegistrationsController#create as HTML
  Parameters: {"utf8"=>"?", "authenticity_token"=>"c8v6fmCFSlJV2v9qClxD46c1wcBU7n78Mk9xWsJm/Ls=", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "customer_attributes"=>{"first_name"=>"test", "last_name"=>"doe"}, "role_ids"=>["", "3"]}, "commit"=>"Sign up"}
Completed   in 9ms

NameError (uninitialized constant Registration): What I can figured out is the route conflict. tutorial #1, use devise custom registrations controller for registering new user. tut#2, added another way to create a new user by super_admin. Below is route listing.

cancel_user_registration GET    /users/cancel(.:format)             {:action=>"cancel", :controller=>"registrations"}
       user_registration POST   /users(.:format)                    {:action=>"create", :controller=>"registrations"}
   new_user_registration GET    /users/register(.:format)           {:action=>"new", :controller=>"registrations"}
  edit_user_registration GET    /users/edit(.:format)               {:action=>"edit", :controller=>"registrations"}
                         PUT    /users(.:format)                    {:action=>"update", :controller=>"registrations"}
                         DELETE /users(.:format)                    {:action=>"destroy", :controller=>"registrations"}
       user_confirmation POST   /users/confirmation(.:format)       {:action=>"create", :controller=>"devise/confirmations"}
   new_user_confirmation GET    /users/confirmation/new(.:format)   {:action=>"new", :controller=>"devise/confirmations"}
                         GET    /users/confirmation(.:format)       {:action=>"show", :controller=>"devise/confirmations"}
                   users GET    /users(.:format)                    {:action=>"index", :controller=>"users"}
                         POST   /users(.:format)                    {:action=>"create", :controller=>"users"}
                new_user GET    /users/new(.:format)                {:action=>"new", :controller=>"users"}
               edit_user GET    /users/:id/edit(.:format)           {:action=>"edit", :controller=>"users"}
                    user GET    /users/:id(.:format)                {:action=>"show", :controller=>"users"}
                         PUT    /users/:id(.:format)                {:action=>"update", :controller=>"users"}
                         DELETE /users/:id(.:format)                {:action=>"destroy", :controller=>"users"}

note matching http verb Post /users and user_registrations path was matched first.

                     POST   /users(.:format)                    {:action=>"create", :controller=>"users"}

   user_registration POST   /users(.:format)                    {:action=>"create", :controller=>"registrations"}

But don’t know what is the best way to avoid this conflict. Can you shed some light on this? I could not figure out from your tutorial.

Thanks in advance,

George

2

2 Answers

1
votes

To avoid routes conflict and avoid any "coding inconvenience" you can use scope:

scope '/admin' do
  resources :users
end

Your routes will then look like this:

                  POST   /admin/users(.:format)          users#create
user_registration POST   /users(.:format)                users/registrations#create
0
votes

To avoid routes conflict, I decide to move controller/views of index/add/update/delete users by admin into an Admin namespace to preserve the restfulness. i.e.

                 POST   /admin/users(.:format)                    {:action=>"create", :controller=>"admin/users"}

   user_registration POST   /users(.:format)                    {:action=>"create", :controller=>"registrations"}

Namespace will cause some coding inconvenience, but that is ok to deal with.