开发者

Different controller actions for POST and GET requests at the same route in Rails

开发者 https://www.devze.com 2023-03-05 01:36 出处:网络
I want to route the same address, e.g.开发者_运维百科, \'http://server/path\' to different controller actions depending on the request type, whether it is a GET or POST request.

I want to route the same address, e.g.开发者_运维百科, 'http://server/path' to different controller actions depending on the request type, whether it is a GET or POST request.

How can I do that in Rails?

Thanks!


get "/path" => "controller#get_action"
post "/path" => "controller#post_action"


I think you could do this:

match '/path' => 'controller#action', :via => :get
match '/path' => 'controller#another_action', :via => :post


Generate a resource using the Rails scaffold and you'll see how it should be done:

./script/generate scaffold Person name:string

EDIT

Got downvoted so maybe I should expand my answer. The scaffold demonstrates how to build a RESTful resource. By convention, a POST will map to the create method in the controller, a GET will map to the index method (or the show method if an ID is present), etc. All you need add to your routes.rb is:

resources :people
0

精彩评论

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