Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Ruby on Rails, how do we print debug info inside of a controller?

In PHP, CGI, or in RoR's View, we can easily print out debug information. How about in the Controller, how can we just say, print "hello world" (to the webpage output) and return to continue with the view, or stop the controller right there?

like image 464
nonopolarity Avatar asked Jul 27 '10 20:07

nonopolarity


People also ask

Which Ruby object method controls how an object is displayed in the console?

irb - Method called on an object when displayed on the console in Ruby - Stack Overflow.

How do I debug in Ruby?

To help deal with bugs, the standard distribution of Ruby includes a debugger. In order to start the Ruby debugger, load the debug library using the command-line option -r debug. The debugger stops before the first line of executable code and asks for the input of user commands.


2 Answers

In the controller you can:

render :text => @some_object.inspect 

But your view won't be rendered.

You could also:

Rails.logger.debug("My object: #{@some_object.inspect}") 

and run tail on log/development.log to see the output.

In the view the recommeneded way is:

<%= debug(@some_object) %> 
like image 94
Papipo Avatar answered Oct 26 '22 14:10

Papipo


Don't know about print, but puts never failed me. Your hello world will be in console and logs and normal flow will continue.
Did I understand you correctly?

like image 23
Nikita Rybak Avatar answered Oct 26 '22 13:10

Nikita Rybak