在 Ruby on Rails 中识别 GET 和 POST 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/152585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:42:19  来源:igfitidea点击:

Identify GET and POST parameters in Ruby on Rails

ruby-on-rails

提问by Swanand

What is the simplest way to identify and separate GET and POST parameters from a controller in Ruby on Rails, which will be equivalent to $_GET and $_POST variables in PHP?

在 Ruby on Rails 中从控制器中识别和分离 GET 和 POST 参数的最简单方法是什么,这相当于 PHP 中的 $_GET 和 $_POST 变量?

采纳答案by Ben Scofield

I don't know of any convenience methods in Rails for this, but you can access the querystring directly to parse out parameters that are set there. Something like the following:

我不知道 Rails 中有什么方便的方法,但是您可以直接访问查询字符串来解析在那里设置的参数。类似于以下内容:

request.query_string.split(/&/).inject({}) do |hash, setting|
  key, val = setting.split(/=/)
  hash[key.to_sym] = val
  hash
end

回答by John Topley

You can use the request.get?and request.post?methods to distinguish between HTTP Gets and Posts.

您可以使用request.get?request.post?方法来区分 HTTP Gets 和 Posts。

回答by wiseland

You can do it using:

您可以使用:

request.POST

request.POST

and

request.GET

request.GET

回答by Ryan Barton

There are three very-lightly-documented hash accessors on the request object for this:

为此,请求对象上有三个很少记录的哈希访问器:

  • request.query_parameters- sent as part of the query string, i.e. after a ?
  • request.path_parameters- decoded from the URL via routing, i.e. controller, action, id
  • request.request_parameters- All params, including above as well as any sent as part of the POST body
  • request.query_parameters- 作为查询字符串的一部分发送,即在 ?
  • request.path_parameters- 通过路由从 URL 解码,即控制器、动作、id
  • request.request_parameters- 所有参数,包括以上以及作为 POST 正文的一部分发送的任何参数

You can use Hash#rejectto get to the POST-only params as needed.

您可以Hash#reject根据需要使用获取仅 POST 参数。

Source: http://guides.rubyonrails.org/v2.3.8/action_controller_overview.htmlsection 9.1.1

来源:http: //guides.rubyonrails.org/v2.3.8/action_controller_overview.html第 9.1.1 节

I looked in an old Rails 1.2.6 app and these accessors existed back then as well.

我查看了一个旧的 Rails 1.2.6 应用程序,这些访问器当时也存在。

回答by webmat

If you want to check the type of request in order to prevent doing anything when the wrong method is used, be aware that you can also specify it in your routes.rb file:

如果您想检查请求的类型以防止在使用错误方法时执行任何操作,请注意您也可以在 routes.rb 文件中指定它:

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

or

或者

map.resources :posts, :conditions => {:method => :post} 

Your PostsController's update method will now only be called when you effectively had a post. Check out the doc for resources.

您的 PostsController 的 update 方法现在只会在您有效发布帖子时调用。查看文档以获取资源

回答by jesse reiss

There is a difference between GET and POST params. A POST HTTP request can still have GET params.

GET 和 POST 参数之间存在差异。POST HTTP 请求仍然可以有 GET 参数。

GET parameters are URL query parameters.

GET 参数是 URL 查询参数。

POST parameters are parameters in the body of the HTTP request.

POST 参数是 HTTP 请求正文中的参数。

you can access these separately from the request.GET and request.POST hashes.

您可以从 request.GET 和 request.POST 哈希单独访问这些。

回答by Navin

request.get?will return boolean true if it is GET method,

request.get?如果是 GET 方法,则返回布尔值 true,

request.post?will return boolean true if it is POST method,

request.post?如果是 POST 方法,将返回布尔值 true,

回答by domgblackwell

You don't need to know that level of detail in the controller. Your routes and forms will cause appropriate items to be added to the params hash. Then in the controller you just access say params[:foo]to get the foo parameter and do whatever you need to with it.

您不需要知道控制器中的详细程度。您的路由和表单将导致将适当的项目添加到 params 哈希中。然后在控制器中,您只需访问 sayparams[:foo]以获取 foo 参数并执行您需要的任何操作。

The mapping between GET and POST (and PUT and DELETE) and controller actions is set up in config/routes.rbin most modern Rails code.

config/routes.rb大多数现代 Rails 代码中都设置了 GET 和 POST(以及 PUT 和 DELETE)和控制器操作之间的映射。

回答by Toby Hede

I think what you want to do isn't very "Rails", if you know what I mean. Your GET requests should be idempotent- you should be able to issue the same GET request many times and get the same result each time.

我认为你想做的不是很“Rails”,如果你知道我的意思。您的 GET 请求应该是幂等的- 您应该能够多次发出相同的 GET 请求并每次都获得相同的结果。

回答by Rod McLaughlin

I think what Jesse Reiss is talking about is a situation where in your routes.rbfile you have

我认为 Jesse Reiss 所说的是一种情况,您的routes.rb文件中有

post 'ctrllr/:a/:b' => 'ctrllr#an_action'

and you POST to "/ctrllr/foo/bar?a=not_foo"POST values {'a' => 'still_not_foo'}, you will have three different values of 'a': 'foo', 'not_foo', and 'still_not_foo'

并且您 POST 到"/ctrllr/foo/bar?a=not_foo"POST values {'a' => 'still_not_foo'},您将拥有三个不同的 'a' 值:'foo'、'not_foo' 和 ' still_not_foo'

'params' in the controller will have 'a' set to 'foo'. To find 'a' set to 'not_foo' and 'still_not_foo', you need to examine request.GETand request.POST

控制器中的“params”将“a”设置为“foo”。要找到设置为 'not_foo' 和 'still_not_foo' 的 'a',您需要检查request.GETrequest.POST

I wrote a gem which distinguishes between these different key=>value pairs at https://github.com/pdxrod/routesfordummies.

我在https://github.com/pdxrod/routesfordummies写了一个 gem 来区分这些不同的 key=>value 对。