开发者

Rails parameters from GET/POST

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


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


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

request.request_method 


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜