Search code examples
htmlruby-on-railsrubyhaml

How can <p> display a variable


Is there any way that I can load a variable in a 'p' tag?

In my test.html.erb:

<p>"#{@variable}"</p>

And in my test_controller.rb

@variable = "Testing"

However, in the html, it displays "#{@variable}".

How should I fix it?


Solution

  • You need to print your code and then to interpolate it, try with:

    <p><%= "#{@variable}" %></p>
    

    Or just <p><%= @variable %></p>, depending on what you want to do, then you can interpolate your variables.

    With <p>"#{@variable}"</p> you're just printing plain html, nothing is being interpreted by Ruby.