Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 935
Michael Moulsdale Avatar asked Nov 13 '14 16:11

Michael Moulsdale


2 Answers

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

<div class="<%= my_class_name %>">
like image 93
Max Williams Avatar answered Sep 28 '22 04:09

Max Williams


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.

like image 37
Simone Carletti Avatar answered Sep 28 '22 04:09

Simone Carletti