Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I print debug messages to the browser console in Ruby on Rails?

If I'm testing my Javascript code, I often use console.log to write messages to the browser console. I find it a convenient place to check for these messages.

Is it possible to output messages to the browser console from Ruby files within your Rails project? (E.g. from a method in the model)

like image 390
Teresa Avatar asked Apr 03 '17 14:04

Teresa


2 Answers

You can use console.log() in your views:

#view.html.erb
<script>
    console.log("Message");
</script>

If you want to log in your Model, try:

Rails.logger.debug "message!!"
like image 154
Alejandro Montilla Avatar answered Sep 18 '22 21:09

Alejandro Montilla


Simple. Just call puts 'your debug message' and it will be printed to where the server is logging. For instance, if you are running the rails server only by running rails s on a terminal, the output of puts will be on this same terminal. If you need more 'power' to debug, you should consider using IDE's like RubyMine to debug your code. Thus, you can place breakpoints and see all the application state.

like image 42
alanpaivaa Avatar answered Sep 19 '22 21:09

alanpaivaa