简体   繁体   English

Rails中的打印和调试功能?

[英]print and debugging functions in rails?

In php one can print_r() anywhere in the view, controller, or model. 在php中,可以在视图,控制器或模型中的任何位置使用print_r()。 Is there anything like that in rails? 铁轨上有类似的东西吗? I tried to_yaml and inspect. 我尝试to_yaml并检查。 They don't seem to print things out from the model. 他们似乎并没有从模型中打印出来。 Is it only allowed to be used in view? 只能在视图中使用吗? If not any example in model or controller? 如果没有模型或控制器中的任何示例?

This doesn't really exist because it's the lest effective way of debugging. 这实际上并不存在,因为这是最有效的调试方法。

Being able to dump output to the browser depends on where you are. 能否将输出转储到浏览器取决于您所在的位置。 It's trivially easy in views, slightly cumbersome in controllers, and too difficult to be worth-while from models. 它在视图中非常容易,在控制器中稍显笨拙,并且很难从模型中获得。

Fortunately, there are much better tools than simply dumping things into the browser. 幸运的是,不是简单地倾倒的东西到浏览器更好的工具。

You can use pry to stop mid-request, open a REPL environment and interactively query or modify the state of your running application. 您可以使用pry停止中级请求,打开REPL环境并以交互方式查询或修改正在运行的应用程序的状态。

If you simply want to trace the flow of execution through output, use the logger: 如果您只想跟踪通过输出执行的流程,请使用记录器:

Rails.logger.info(my_object.inspect)

Normally you'll identify problems in your model, controller or integration tests long before it becomes an issue. 通常,您会在模型,控制器或集成测试成为问题之前很久就发现问题。 In that context you can use puts to output whatever you want when instrumenting bits of code and it will show up in your test output: 在这种情况下,您可以使用puts在检测代码puts时输出所需的任何内容,它将显示在测试输出中:

puts object.inspect

Within the Rails operational environment you can use Rails.logger : 在Rails的操作环境中,您可以使用Rails.logger

Rails.logger.debug(object.inspect)

This will show up in log/development.org where you can see what's going on. 这将显示在log/development.org ,您可以在其中查看发生的情况。 It's best to leave this at debug level so it doesn't clutter up your production logs if left in by accident. 最好将其保留在debug级别,以免意外遗留生产日志。

Short answer is, you can't. 简短的答案是,你不能。 At least not in one line. 至少没有一行。 And not only because this is a violation of MVC, there are also practical reasons that prevent this. 不仅因为这违反了MVC,还有一些实际的原因可以阻止这种情况。

There is no reliable way to output a bunch of data in an arbitrary format and keep it valid. 没有可靠的方法可以以任意格式输出一堆数据并保持其有效。 Outputting it in JSON views may easily result in invalid data. 在JSON视图中输出它很容易导致无效数据。 So if your debug data can only be handled by a browser, that output should only be specified in views for browsers. 因此,如果调试数据只能由浏览器处理,则只能在浏览器的视图中指定该输出。 Even if none other exist, separate concerns . 即使没有其他问题也要分开关注

There is a substitute, of course. 当然有替代品。 Rails 4.2.0 ships an app template with web_console . Rails 4.2.0附带了一个带有web_console的应用程序模板。 All you need to start using it is add a call to console in your views somewhere, like the app's general layout file. 您开始使用它所需要的只是在您的视图中某个地方添加一个console调用,例如该应用程序的常规布局文件。 If that's actually ERB, add this line below: 如果这实际上是ERB,请在下面添加以下行:

<%= console %>

And wherever it appears, you have a REPL in the context of the currently rendered view, where you can easily inspect objects and even perform actions that change your data. 无论出现在何处,您都将在当前渲染的视图的上下文中拥有一个REPL,您可以在其中轻松检查对象,甚至执行更改数据的操作。

There is also a variety of methods to output data into the server's console or log file. 还有多种方法可以将数据输出到服务器的控制台或日志文件中。 They've been listed in other answers. 它们已在其他答案中列出。 I'll add a little to the solution involving logger. 我将在涉及记录器的解决方案中添加一些内容。

Rails Panel . 导轨面板 It's a Chrome extension that adds another tab to Chrome Dev Tools (that show up behind F12) named "Rails". 这是一个Chrome扩展程序,它为Chrome开发工具(位于F12后面)添加了另一个名为“ Rails”的标签。 For it to work, you need to add a meta_request gem to your app (make sure it's in group development !). 为了使其正常工作,您需要向您的应用程序添加一个meta_request gem(确保它在组development !)。 Once working, it will show loads of data about how the page was processed: 一旦工作,它将显示有关页面处理方式的大量数据:

  • Time spent fetching data, rendering it 提取数据,渲染数据所花费的时间
  • Parameters for the given request 给定请求的参数
  • Executed DB queries, duration and lines they've been triggered by 执行的数据库查询,持续时间和它们触发的行
  • View files involved 查看涉及的文件
  • Log entries emitted on this request and what triggered that 在此请求上发出的日志条目以及触发该请求的原因
  • Errors encountered 遇到的错误

This one and some other debugging things are discussed in this Railscast . 这一项和其他一些调试工作将在此Railscast中讨论。

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

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