开发者

Override method for default RESTFUL Routes in Rails

Given a line something like below in routes.rb

map.resources :users

The routes generated might be 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 G开发者_Go百科ET    /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 any way to change the default HTTP method of POST /users mapping to {:controller=>"users", :action=>"create"} to the HTTP method being used to be a PUT instead?

rake routes would then 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 wouldn't be correct for RESTful routing, I'm just wondering if it is possible to change the HTTP methods used by these routes.


You can explicitly add a route to accept a /users with PUT to create users but it won't replace the existing POST route.

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

You can also add a new route for creating users by adding :member => { :create => :put } which will result in a route like this:

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

but I appreciate that that doesn't address exactly what you are asking!


Defining

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

Will do it for :users. I am not sure about a default override like with method names in action controller like:

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

Which is what you may have been asking.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜