Ninja
Ninja

Reputation: 1052

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

I'm quite a rails beginner, but I am trying to see the contents of a Ruby variable from within my Rails View (html.haml file). I tried to print out the variable in ruby (thinking it would turn out in the terminal), but didn't get any results. Any suggestions?

I know about the Rails Debugger, but would rather prefer printing out my variables using inspect instead.

Upvotes: 5

Views: 11630

Answers (3)

d1jhoni1b
d1jhoni1b

Reputation: 8035

In case of requiring more detailed debug messages you could try something like this

view.haml

    = debug "my_variable_value: #{@my_variable}"

controller.rb

    def index
      @my_variable = true
    end

Upvotes: 0

joseramonc
joseramonc

Reputation: 1931

You can use:

= debug @object

It's going to print and highlight the variable in the view.

Upvotes: 5

user1454117
user1454117

Reputation:

You can use the puts method from within your views to output information to your server console. You should be able to do the following using Haml from anywhere in your view:

- puts @my_variable.inspect

Upvotes: 6

Related Questions