I have a Rails 3 application and I want to print in the view the request parameters. How do I do it?
我有一个Rails 3应用程序,我想在视图中打印请求参数。我该怎么做呢?
Edit:
编辑:
The purpose is to see what is being sent in a Form.
其目的是查看以表单发送的内容。
8 个解决方案
#1
42
I would use debug(params)
. That will give you a nicely formatted view of them.
我将使用调试(params)。这会给你一个很好的格式化视图。
#2
71
If you wanted to print all of the parameters, the easiest way would be to use the inspect
如果要打印所有参数,最简单的方法是使用inspect
puts params.inspect
or better, use the Rails logger
或者更好,使用Rails日志记录器
Rails.logger.debug params.inspect
In your html/ERB, you can use
在html/ERB中,您可以使用
<%= params.inspect %>
#3
10
Parameters are stored in the params
hash. For example, if there was a title
parameter, you could display it in your view using <%= params[:title] %>
.
参数存储在params散列中。例如,如果有一个title参数,可以使用<%= params[:title] %>在视图中显示它。
#4
9
Learnt this from the Ruby Hero James Edward Gray II on this episode of Ruby Rogues podcast which I highly recommend. raise is a swiss army knife for inspecting anything in your Rails code which prints it nicely on your browser.
这是我从Ruby英雄James Edward Gray II在Ruby Rogues播客上学到的,我强烈推荐。raise是一个瑞士军刀,用于检查你的Rails代码中的任何东西,它在你的浏览器上很好地打印出来。
raise params.inspect
提高params.inspect
#5
3
pretty_print
also can be useful, in ruby and rails apps both
pretty_print在ruby和rails应用程序中也很有用
pp params
pretty_print doc: http://ruby-doc.org/stdlib-2.1.0/libdoc/pp/rdoc/PP.html
pretty_print条件文档:http://ruby-doc.org/stdlib-2.1.0/libdoc/pp/rdoc/PP.html
#6
1
You can use for models, controllers, etc.
可以用于模型、控制器等。
puts YAML::dump(params)
Source: Ruby / Rails alternative to PHP print_r() and var_dump()
源代码:Ruby / Rails替代PHP print_r()和var_dump()
For views:
观点:
DebugHelper’s debug(object)
In your case:
在你的例子:
DebugHelper’s debug(params)
#7
1
Source: http://guides.rubyonrails.org/getting_started.html#creating-articles
来源:http://guides.rubyonrails.org/getting_started.html创建
When a form is submitted, the fields of the form are sent to Rails as parameters. These parameters can then be referenced inside the controller actions, typically to perform a particular task. To see what these parameters look like, change the create action to this:
提交表单时,表单的字段作为参数发送到Rails。然后可以在控制器动作中引用这些参数,通常用于执行特定的任务。要查看这些参数是什么样子,请将create操作改为:
def create
render plain: params[:article].inspect
end
The response when POST'ing a form to the targeted #create route would be a plain-text hash output of params[:article]
将表单发送到目标#创建路径时的响应将是params的纯文本散列输出[:文章]
#8
1
Something like
类似的
<%= params.inspect %>
works, but what I would like to add is the following gem and Chrome plugin which was literally an eye-opener.
工作,但我想补充的是下面的gem和Chrome插件,真是让人大开眼界。
I am putting it here because I think it will help people check out params hashes, see SQL queries or view Errors.
我之所以把它放在这里,是因为我认为它将帮助人们检查params散列,查看SQL查询或查看错误。
#1
42
I would use debug(params)
. That will give you a nicely formatted view of them.
我将使用调试(params)。这会给你一个很好的格式化视图。
#2
71
If you wanted to print all of the parameters, the easiest way would be to use the inspect
如果要打印所有参数,最简单的方法是使用inspect
puts params.inspect
or better, use the Rails logger
或者更好,使用Rails日志记录器
Rails.logger.debug params.inspect
In your html/ERB, you can use
在html/ERB中,您可以使用
<%= params.inspect %>
#3
10
Parameters are stored in the params
hash. For example, if there was a title
parameter, you could display it in your view using <%= params[:title] %>
.
参数存储在params散列中。例如,如果有一个title参数,可以使用<%= params[:title] %>在视图中显示它。
#4
9
Learnt this from the Ruby Hero James Edward Gray II on this episode of Ruby Rogues podcast which I highly recommend. raise is a swiss army knife for inspecting anything in your Rails code which prints it nicely on your browser.
这是我从Ruby英雄James Edward Gray II在Ruby Rogues播客上学到的,我强烈推荐。raise是一个瑞士军刀,用于检查你的Rails代码中的任何东西,它在你的浏览器上很好地打印出来。
raise params.inspect
提高params.inspect
#5
3
pretty_print
also can be useful, in ruby and rails apps both
pretty_print在ruby和rails应用程序中也很有用
pp params
pretty_print doc: http://ruby-doc.org/stdlib-2.1.0/libdoc/pp/rdoc/PP.html
pretty_print条件文档:http://ruby-doc.org/stdlib-2.1.0/libdoc/pp/rdoc/PP.html
#6
1
You can use for models, controllers, etc.
可以用于模型、控制器等。
puts YAML::dump(params)
Source: Ruby / Rails alternative to PHP print_r() and var_dump()
源代码:Ruby / Rails替代PHP print_r()和var_dump()
For views:
观点:
DebugHelper’s debug(object)
In your case:
在你的例子:
DebugHelper’s debug(params)
#7
1
Source: http://guides.rubyonrails.org/getting_started.html#creating-articles
来源:http://guides.rubyonrails.org/getting_started.html创建
When a form is submitted, the fields of the form are sent to Rails as parameters. These parameters can then be referenced inside the controller actions, typically to perform a particular task. To see what these parameters look like, change the create action to this:
提交表单时,表单的字段作为参数发送到Rails。然后可以在控制器动作中引用这些参数,通常用于执行特定的任务。要查看这些参数是什么样子,请将create操作改为:
def create
render plain: params[:article].inspect
end
The response when POST'ing a form to the targeted #create route would be a plain-text hash output of params[:article]
将表单发送到目标#创建路径时的响应将是params的纯文本散列输出[:文章]
#8
1
Something like
类似的
<%= params.inspect %>
works, but what I would like to add is the following gem and Chrome plugin which was literally an eye-opener.
工作,但我想补充的是下面的gem和Chrome插件,真是让人大开眼界。
I am putting it here because I think it will help people check out params hashes, see SQL queries or view Errors.
我之所以把它放在这里,是因为我认为它将帮助人们检查params散列,查看SQL查询或查看错误。