简体   繁体   English

如何在Ruby on Rails中执行期间确定控制器变量的值?

[英]How to determine the value of a controller variable during execution in Ruby on Rails?

What is the best way for me to determine a controller variable's value during execution? 在执行期间确定控制器变量值的最佳方法是什么?

For example, is there a way I can insert a break in the code, and cause the value of the variable to be output to the screen (or the log)? 例如,有没有办法可以在代码中插入一个中断,并将变量的值输出到屏幕(或日志)?

Yes. 是。 The easiest way is to raise the value as a string. 最简单的方法是将值提升为字符串。 Like so: raise @foo.to_s 像这样: raise @foo.to_s

Or, you can install the debugger ( gem install ruby-debug ), and then start the development server with the --debugger flag. 或者,您可以安装调试器( gem install ruby-debug ),然后使用--debugger标志启动开发服务器。 Then, in your code, call the debugger instruction. 然后,在您的代码中,调用debugger指令。

Inside the debugger prompt, you have many commands, including p to print the value of a variable. 在调试器提示符中,您有许多命令,包括p来打印变量的值。

Update: here's a bit more about ruby-debug . 更新:这里有关于ruby-debug的更多信息

If you have a controller instance variable named @foo , then in your controller you can simply do something like: 如果您有一个名为@foo的控制器实例变量,那么在您的控制器中,您可以执行以下操作:

logger.debug "@foo is: #{@foo}"

Additionally, you can output the value in your view template using: 此外,您可以使用以下方法在视图模板中输出值:

<%= debug @foo %>

I prefer using the inspect method like so: 我更喜欢使用这样的检查方法:

raise @foo.inspect

It has more information than to_s, like the attribute values. 它具有比to_s更多的信息,如属性值。

Summary from Jordi Bunster, John Topley, and Jaryl: Jordi Bunster,John Topley和Jaryl的总结:

I. Quick and dirty way: I.快捷方式:

raise @foo.inspect

in your controller. 在你的控制器中。 Or 要么

<% raise @foo.inspect %>

in your view. 在你看来。

II. II。 Proper logging to you development.log : 正确登录您的development.log

logger.debug "@foo == #{@foo.inspect}"

III. III。 Full-fledged debugging : 完整的调试

Install the debugger ( gem install ruby-debug ), and then start the development server with the --debugger flag. 安装调试器( gem install ruby-debug ),然后使用--debugger标志启动开发服务器。 Then, in your code, call the debugger instruction. 然后,在您的代码中,调用debugger指令。

Inside the debugger prompt, you have many commands, including p to print the value of a variable. 在调试器提示符中,您有许多命令,包括p来打印变量的值。

  1. Add pry-moves to Gemfile: gem 'pry-moves' 添加pry - move到Gemfile:gem'prey gem 'pry-moves'
  2. Insert binding.pry where you want to stop binding.pry插入要停止的位置
  3. Type variable's name to see its value 输入变量的名称以查看其值

Then continue by typing c , move to next line with n or perform other debugging actions until you will resolve the issue. 然后继续输入c ,使用n移动到下一行或执行其他调试操作,直到您将解决问题。

Raising an exception is the fastest way if you just need to look at a value, but it's worth the time to learn how to use the debugger properly. 如果您只需要查看值,则提出异常是最快的方法,但是值得花时间学习如何正确使用调试器。 It's rare that you would only need to just see the value of a variable, you are likely trying to find a bug in your code, and that's what a debugger is for. 您很少需要只看到变量的值,您可能正试图在代码中找到错误,这就是调试器的用途。

Sending the info to the development log is slower than either of the other two options here so far if you learn how to use the debugger (who wants to read through log files). 到目前为止,如果您学习如何使用调试器(谁想要读取日志文件),那么将信息发送到开发日志的速度要慢于其他两个选项。 Use the logger for production, you are going to want to see what the value was when somebody calls you up and says everything is broken. 使用记录器进行生产,您将希望看到有人打电话给您并说一切都坏了的价值。

Well, I usually prefer the standard error output 好吧,我通常更喜欢标准的错误输出

$stderr.print("whatever") $ stderr.print( “什么”)

Its simple and does the job. 它简单而且干得好。

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

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