Default Override Method RESTFUL Routes in Rails

For a line like below in routes.rb

map.resources :users

The created routes may look something like this:

      users GET    /users(.:format)           {:controller=>"users", :action=>"index"}
            POST   /users(.:format)           {:controller=>"users", :action=>"create"}
   new_user GET    /users/new(.:format)       {:controller=>"users", :action=>"new"}
  edit_user GET    /users/:id/edit(.:format)  {:controller=>"users", :action=>"edit"}
       user GET    /users/:id(.:format)       {:controller=>"users", :action=>"show"}
            PUT    /users/:id(.:format)       {:controller=>"users", :action=>"update"}
            DELETE /users/:id(.:format)       {:controller=>"users", :action=>"destroy"}

Is there a way to change the default HTTP method POST /usersto {:controller=>"users", :action=>"create"}to the HTTP method that is used instead PUT?

rake routes will generate something like this:

      users GET    /users(.:format)           {:controller=>"users", :action=>"index"}
            PUT    /users(.:format)           {:controller=>"users", :action=>"create"}
   new_user GET    /users/new(.:format)       {:controller=>"users", :action=>"new"}
  edit_user GET    /users/:id/edit(.:format)  {:controller=>"users", :action=>"edit"}
       user GET    /users/:id(.:format)       {:controller=>"users", :action=>"show"}
            PUT    /users/:id(.:format)       {:controller=>"users", :action=>"update"}
            DELETE /users/:id(.:format)       {:controller=>"users", :action=>"destroy"}

I understand that this would be wrong for RESTful routing, I'm just wondering if the HTTP methods used by these routes can be changed.

+5
source share
2 answers

You can explicitly add a route to accept /userswith PUT to create users, but it will not replace the existing POST route.

map.connect '/users(.:format)', 
   :controller => 'users', 
   :action => 'create', 
   :conditions => { :method => :put }

, :member => { :create => :put }, :

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

, , !

+2

map.resources :users, :member =>{:create => :put}

:users. , , , :

config.action_controller.resources_path_names = { :new => "create", 
        :edit => "change" }

, , , .

+1

All Articles