Zequez
Zequez

Reputation: 3499

How do I output to website instead of console for debugging in Rails?

Well the title explains pretty much everything. I want to use something like

p something

And be able to see the output in the website, just for the sake of debugging.

Upvotes: 2

Views: 280

Answers (2)

hayesgm
hayesgm

Reputation: 9096

I would suggest piggy-backing off of the Rails flash maps. In a controller, use the following:

 flash[:log] = "<li>Log: #{log_info}</li>"

Then in your view (possibly in your layout) use:

 <% if !flash[:log].blank? && RAILS_ENV != 'production' %>
   <ul class="logs">
     <%= flash[:log] %>
   </ul>
 <% end %>

That should put it out on your page!

You can also put the following in ApplicationController

 def log(msg)
   flash[:log] ||= ""
   flash[:log] += "<li>#{Time.new.to_s} - #{msg}</li>"
 end

Then simply call from any controller:

 log("Hi stackoverflow.")

Or from any view:

 @controller.log("Hi there, you!")

Upvotes: 3

user229044
user229044

Reputation: 239311

I suspect you're coming from PHP, because this is a very PHP-esque thing to want to do. Standard output isn't sent to the browser in Rails, something PHP developers take for granted.

If you want to see something in browser, you need to render it.

From a view you can use the debug helper to "vardump" a value:

<%= debug something %>

From a controller you can quickly see the value of a variable using render :inline => p(something) or render :inline => something.inspect. Make sure you return afterwards or otherwise prevent yourself from reaching a second render call.

You should get used tailing your log files, and making use of the Rails logger.

Upvotes: 6

Related Questions