简体   繁体   中英

print and debugging functions in rails?

In php one can print_r() anywhere in the view, controller, or model. Is there anything like that in rails? I tried to_yaml and inspect. 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.

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 object.inspect

Within the Rails operational environment you can use Rails.logger :

Rails.logger.debug(object.inspect)

This will show up in log/development.org where you can see what's going on. It's best to leave this at debug level so it doesn't clutter up your production logs if left in by accident.

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.

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. 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 . All you need to start using it is add a call to console in your views somewhere, like the app's general layout file. If that's actually ERB, add this line below:

<%= 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.

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". For it to work, you need to add a meta_request gem to your app (make sure it's in group 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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