Michael Moulsdale
Michael Moulsdale

Reputation: 1488

rails variable within div class name

I have just upgraded to Rails 4 (which may or may not be the issue.

In the past if I have wanted to use a variable within a div class name I have done something like the following.

<% my_class_name = "hello" %>
<div class = "#{my_class_name}">
..
</div>

(Obviously the variable would be set in the controller or be an instance of a collection)

However, when I do that now the output I see in the page source is exactly what has been wriiten i.e.

<div class = "#{my_class_name}">
and NOT
<div class = "hello">

Has something changed in rails 4 where this syntax is no longer valid? Or any other hints, greatly appreciated.

Interestingly when I use the following in a helper method, all is well...

content_tag(:div, class: "#{divclass}") do

Upvotes: 8

Views: 5829

Answers (3)

Max Williams
Max Williams

Reputation: 32955

You need to use an erb tag in your erb templates:

<div class="<%= my_class_name %>">

Upvotes: 17

ppascualv
ppascualv

Reputation: 1137

<% my_class_name = "hello" %>
<div class = <%= my_class_name %>>
..
</div

Use <%= %> instead of #{}

Upvotes: 0

Simone Carletti
Simone Carletti

Reputation: 176562

You need to enter the ERB/Ruby context using the <% tag.

<% my_class_name = "hello" %>
<div class="<%= my_class_name %>">
..
</div>

#{} is an interpolation inside an ERB/Ruby context.

Upvotes: 3

Related Questions