How do I print the contents of a ruby ​​variable in the view of my Rails application?

I'm new to rails, but I'm trying to see the contents of a Ruby variable from my Rails View (html.haml file). I tried to print the variable in ruby ​​(thinking that it would work in the terminal), but did not get any results. Any suggestions?

I know about the Rails Debugger, but would rather print my variables using validation instead.

+4
source share
3 answers

You can use the method putsfrom your views to display information on the server console. You can use Haml from anywhere in your view:

- puts @my_variable.inspect
+2
source

:

= debug @object

.

+3

If you need more detailed debugging messages, you can try something like this

view.haml = debug "my_variable_value: #{@my_variable}"

controller.rb

def index @my_variable = true end

0
source

All Articles