Ruby-on-rails Rails 参数解释了?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6885990/
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
Rails params explained?
提问by Dru
Could anyone explain paramsin Rails controller: where they come from, and what they are referencing?
任何人都可以params在 Rails 控制器中解释:它们来自哪里,它们引用了什么?
def create
@vote = Vote.new(params[:vote])
item = params[:vote][:item_id]
uid = params[:vote][:user_id]
@extant = Vote.find(:last, :conditions => ["item_id = ? AND user_id = ?", item, uid])
last_vote_time = @extant.created_at unless @extant.blank?
curr_time = Time.now
end
I would like to be able to read this code line-by-line and understand what's going on.
我希望能够逐行阅读此代码并了解发生了什么。
回答by David Grayson
The params come from the user's browser when they request the page. For an HTTP GET request, which is the most common, the params are encoded in the url. For example, if a user's browser requested
参数来自用户请求页面时的浏览器。对于最常见的 HTTP GET 请求,参数编码在 url 中。例如,如果用户的浏览器请求
http://www.example.com/?foo=1&boo=octopus
http://www.example.com/?foo=1&boo=octopus
then params[:foo]would be "1" and params[:boo]would be "octopus".
然后params[:foo]将是“1”,params[:boo]将是“章鱼”。
In HTTP/HTML, the params are really just a series of key-value pairs where the key and the value are strings, but Ruby on Rails has a special syntax for making the params be a hash with hashes inside. For example, if the user's browser requested
在 HTTP/HTML 中,参数实际上只是一系列键值对,其中键和值是字符串,但 Ruby on Rails 有一种特殊的语法,可以使参数成为内部带有散列的散列。例如,如果用户的浏览器请求
http://www.example.com/?vote[item_id]=1&vote[user_id]=2
http://www.example.com/?vote[item_id]=1&vote[user_id]=2
then params[:vote]would be a hash, params[:vote][:item_id]would be "1" and params[:vote][:user_id]would be "2".
那么params[:vote]将是一个散列,params[:vote][:item_id]将是“1”,params[:vote][:user_id]将是“2”。
The Ruby on Rails params are the equivalent of the $_REQUEST array in PHP.
Ruby on Rails 参数相当于PHP中的$_REQUEST 数组。
回答by hammar
As others have pointed out, paramsvalues can come from the query string of a GET request, or the form data of a POST request, but there's also a third place they can come from: The path of the URL.
正如其他人指出的那样,params值可以来自 GET 请求的查询字符串或 POST 请求的表单数据,但它们也可以来自第三个地方:URL 的路径。
As you might know, Rails uses something called routes to direct requests to their corresponding controller actions. These routes may contain segments that are extracted from the URL and put into params. For example, if you have a route like this:
您可能知道,Rails 使用称为路由的东西将请求定向到其相应的控制器操作。这些路由可能包含从 URL 中提取并放入params. 例如,如果您有这样的路线:
match 'products/:id', ...
Then a request to a URL like http://example.com/products/42will set params[:id]to 42.
然后对类似 URL 的请求http://example.com/products/42将设置params[:id]为42.
回答by thejaz
Params contains the following three groups of parameters:
Params 包含以下三组参数:
- User supplied parameters
- GET (http://domain.com/url?param1=value1¶m2=value2will set params[:param1] and params[:param2])
- POST (e.g. JSON, XML will automatically be parsed and stored in params)
- Note: By default, Rails duplicates the user supplied parameters and stores them in params[:user] if in UsersController, can be changed with wrap_parameters setting
- Routing parameters
match '/user/:id'in routes.rb will set params[:id]
- Default parameters
params[:controller]andparams[:action]is always available and contains the current controller and action
- 用户提供的参数
- 获取(http://domain.com/url?param1=value1¶m2=value2将设置 params[:param1] 和 params[:param2])
- POST (例如 JSON、XML 会自动解析并存储在 params 中)
- 注意:默认情况下,Rails 复制用户提供的参数并将它们存储在 params[:user] 中,如果在 UsersController 中,可以使用 wrap_parameters 设置进行更改
- 路由参数
match '/user/:id'在 routes.rb 将设置 params[:id]
- 默认参数
params[:controller]并且params[:action]始终可用并包含当前控制器和操作
回答by Smar
Basically, parameters are user specified data to rails application.
基本上,参数是用户指定的 Rails 应用程序数据。
When you post a form, you do it generally with POST request as opposed to GET request. You can think normal rails requests as GET requests, when you browse the site, if it helps.
当您发布表单时,您通常使用 POST 请求而不是 GET 请求来执行此操作。当您浏览站点时,如果有帮助,您可以将正常的 rails 请求视为 GET 请求。
When you submit a form, the control is thrown back to the application. How do you get the values you have submitted to the form? paramsis how.
当您提交表单时,控件将返回给应用程序。你如何获得你提交给表单的值?params是怎样。
About your code. @vote = Vote.new params[:vote]creates new Vote to database using data of params[:vote]. Given your form user submitted was named under name :vote, all data of it is in this :vote field of the hash.
关于你的代码。@vote = Vote.new params[:vote]使用 params[:vote] 的数据创建新的投票到数据库。鉴于您提交的表单用户名为 :vote,它的所有数据都在哈希的这个 :vote 字段中。
Next two lines are used to get item and uid user has submitted to the form.
接下来的两行用于获取 item 和 uid 用户已提交到表单。
@extant = Vote.find(:last, :conditions => ["item_id = ? AND user_id = ?", item, uid])
finds newest, or last inserted, vote from database with conditions item_id = item and user_id = uid.
在条件 item_id = item 和 user_id = uid 的情况下,从数据库中查找最新或最后插入的投票。
Next lines takes last vote time and current time.
下一行需要上次投票时间和当前时间。
回答by Margotte
On the Rails side, paramsis a method that returns an ActionController::Parametersobject.
See https://stackoverflow.com/a/44070358/5462485
在 Rails 方面,params是一个返回ActionController::Parameters对象的方法。见https://stackoverflow.com/a/44070358/5462485

