8

I'm fairly new to Rails and am writing a login form. I have used form_tag to pass through the user's submission to the account controller. Now, I don't want the user to be able to enter their login details through a GET request, so how can I check that a certain param is either a GET or POST parameter?

Thanks in advance

alex
  • 95
  • 1
  • 1
  • 4
  • Also see answers to this question: [http://stackoverflow.com/questions/152585/identify-get-and-post-parameters-in-ruby-on-rails][1] [1]: http://stackoverflow.com/questions/152585/identify-get-and-post-parameters-in-ruby-on-rails – Ryan Barton Mar 09 '12 at 22:46

3 Answers3

23

In Rails you don't have specific POST or GET parameters. You do have a POST or GET request. You can check it like this in your controller:

request.post?

or you can check for other HTTP verbs: GET, PUT and DELETE:

request.get?
request.put?
request.delete?

For more info, check this piece of the documentation: http://railsapi.com/doc/rails-v2.3.8/classes/ActionController/Request.html

Ariejan
  • 10,910
  • 6
  • 43
  • 40
  • 3
    In Rails parameters are always accessible through `params[:name]` no matter if they were POSTed or GETed. – Ariejan Oct 26 '10 at 12:16
  • 1
    The link is down, the official documentation was mover here http://api.rubyonrails.org/classes/ActionDispatch/Request.html – ılǝ Oct 24 '13 at 08:48
2

If what you need is to know the HTTP verb you can ask directly to request:

request.request_method 
hcarreras
  • 4,442
  • 2
  • 23
  • 32
0

You could of course POST to a url that included a query parameter, so the selected answer might not be what you're looking for. Try checking if the parameter exists in the request arrays:

if request.GET.include? "param_name"
  # do something
end

There's also request.POST and there are aliases (query_parameters for GET and request_parameters for POST) for both in ActionDispatch::Request:

http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-GET

typeoneerror
  • 55,990
  • 32
  • 132
  • 223