Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to only allow POST Requests in Rails?

How do I prevent users from calling a method "doAction" in my controller using GET requests? I only want it to be called using POST requests?

I heard I need to use "verify" but I'm unsure where to put that code, or how I need to use it.

like image 984
Henley Avatar asked Dec 16 '22 10:12

Henley


2 Answers

You can specify which methods are allowed for any action in your routes.rb.

Rails 2:

map.connect '/posts/doAction', :controller => 'posts,
                               :action     => 'doAction',
                               :conditions => { :method => :post }

Rails 3:

match 'posts/doAction' => "posts#doAction', :via => :post

post 'posts/doAction', :to => "posts#doAction'
like image 84
theIV Avatar answered Dec 30 '22 05:12

theIV


You can add a constraint to the route in routes.rb.

match "/doAction" => "controller#doAction", :via => :post

Refer to http://guides.rubyonrails.org/routing.html for more.

like image 35
Soliah Avatar answered Dec 30 '22 05:12

Soliah