Ruby-on-rails Rails:如何打印请求参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4837859/
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-03 00:14:40  来源:igfitidea点击:

Rails: How do I print the request parameters?

ruby-on-rails

提问by Nerian

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.

目的是查看表单中发送的内容。

采纳答案by Andrew

I would use debug(params). That will give you a nicely formatted view of them.

我会使用debug(params). 这将为您提供格式良好的视图。

回答by Rob Di Marco

If you wanted to print all of the parameters, the easiest way would be to use the 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 %>

回答by Kibet Yegon

Learnt this from the Ruby Hero James Edward Gray II on this episode of Ruby Rogues podcastwhich I highly recommend. raiseis a swiss army knife for inspecting anything in your Rails code which prints it nicely on your browser.

在 我强烈推荐的这一集 Ruby Rogues 播客中,从 Ruby 英雄 James Edward Gray II 那里了解到这一点。raise是一把瑞士军刀,用于检查 Rails 代码中的任何内容,并在浏览器上很好地打印出来。

raise params.inspect

提高 params.inspect

回答by Dylan Markow

Parameters are stored in the paramshash. For example, if there was a titleparameter, you could display it in your view using <%= params[:title] %>.

参数存储在params哈希中。例如,如果有一个title参数,您可以使用<%= params[:title] %>.

回答by alexts

Something like

就像是

<%= params.inspect %>

works, but what I would like to add is the following gem and Chrome pluginwhich 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 查询或查看错误。

回答by itsnikolay

pretty_printalso 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

漂亮打印文档:http://ruby-doc.org/stdlib-2.1.0/libdoc/pp/rdoc/PP.html

回答by wes.hysell

Source: http://guides.rubyonrails.org/getting_started.html#creating-articles

来源:http: //guides.rubyonrails.org/getting_started.html#creating-articles

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。然后可以在控制器操作中引用这些参数,通常用于执行特定任务。要查看这些参数的样子,请将创建操作更改为:

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]

将表单发布到目标 #create 路由时的响应将是纯文本哈希输出 params[:article]

回答by d.danailov

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)