简体   繁体   English

Rails参数(url_params vs form_data)

[英]Rails params (url_params vs form_data)

This is just a question out of curiosity. 出于好奇,这只是一个问题。

Rails is magical in the way that it does a lot of things for us, but it comes with a curse of knowledge. Rails可以为我们做很多事情,这是神奇的,但是它伴随着知识的诅咒。 When Rails receives an http request, we can access the inputs from client via params[]. 当Rails收到一个http请求时,我们可以通过params []从客户端访问输入。 However, I notice that params can accept inputs from both the url_params, and the form_data. 但是,我注意到参数可以接受url_params和form_data的输入。 For eg: 例如:

# Get  users/:id (param comes from url)
# Post users     (param comes from form)

Is there a rule to how params[] works? params []的工作方式是否有规则? Will Rails just put all the parameters from url and form to params[]? Rails会将所有参数从url和form放入params []吗?

In the case of NodeJS, there is a distinction between 对于NodeJS,两者之间存在区别

request.params
request.body
request.query

The answer can be found in the Rails Guides, in the chapter about Action Controller Overview -> Parameters : 答案可以在《 Rails指南》的“动作控制器概述->参数 ”一章中找到:

Rails does not make any distinction between query string parameters and POST parameters, and both are available in the params hash in your controller: Rails在查询字符串参数和POST参数之间没有任何区别,两者都可以在控制器的params哈希中使用:

What are params? 什么是参数?

params are nothing but the parameters which are send to your controller when you send a HTTP request from your browser. 参数只是从浏览器发送HTTP request时发送到控制器的参数。

Types of params? 参数类型?

If you look at rail guides it says 如果您看一下rail guides它会说

There are two kinds of parameters possible in a web application. The first are parameters that are sent as part of the URL, called query string parameters. The query string is everything after "?" in the URL. The second type of parameter is usually referred to as POST data. This information usually comes from an HTML form which has been filled in by the user

Is there a rule to how params works? 有没有关于参数如何工作的规则?

As @zwippie pointed out rails doesn't make any distinction whether your params are coming from a form or a query string, but they do differ in the way rails put these params in a hash and hence different ways to access them in controller 正如@zwippie指出的那样,无论您的参数来自表单还是查询字符串,rails都没有任何区别, 但是它们确实在Rails将这些参数放入哈希中的方式不同,因此在控制器中访问它们的方式也有所不同

For query string: 对于查询字符串:

If your url is something like: 如果您的网址是这样的:

http://www.example.com/?vote[item_id]=1&vote[user_id]=2

then your params will look like: 那么您的参数将如下所示:

{"item_id" => "1", "user_id" => "2"}

and hence you can access them in your controller by params[:item_id] and params[:user_id] 因此,您可以通过params[:item_id] and params[:user_id]在控制器中访问它们

For POST data or from a form: 对于POST数据或表单:

Lets say your form is like 可以说您的表格就像

<%= form_for @person do |f| %>
  <%= f.label :first_name %>:
  <%= f.text_field :first_name %><br />

  <%= f.label :last_name %>:
  <%= f.text_field :last_name %><br />

  <%= f.submit %>
<% end %>

and when you submit your form the parameters will look like this 当您提交表单时,参数将如下所示

{"person"=>   {"first_name"=>"xyz", "last_name"=>"abc"}}

notice how a form has nested your parameters in a hash so to access them in your controller you'll have to do params[:person] and to get individual values you can do params[:person][:first_name] 请注意, 表单是如何将参数嵌套在哈希中的,因此要在控制器中访问它们,您必须执行params[:person]并获取单个值,您可以执行params[:person][:first_name]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM