6

I've rewritten my html code with rails content_tags and now I want to add if-statements to them.

Before I had:

<% if ... %>
    <div class='...'>
    ...
    </div>
<% end %>

Now I have 2 types of this block:

<%= content_tag(:div, ..., class: 'some_class') if ... %>

This works ok. But when I try to add if-statement to do-end block it fails:

<%= content_tag(:div, class: 'some_class') if ... do %>
    ...
<% end %>

I'm seeing this, instead of div content:

<div>{:class=>"some_class"}</div>

Thanks!

Peter Tretyakov
  • 3,094
  • 6
  • 33
  • 46

2 Answers2

15

You have to use like :

<%if ... %>
    <%= content_tag(:div, class: 'some_class') do %>
    ...
    <% end %>
<% end %>

Or using if statement after the block :

<%= content_tag(:div, class: 'some_class') do %>
    ...
<% end if .......%>
Muntasim
  • 6,371
  • 3
  • 39
  • 68
4

Try

<%= content_tag(:div, class: 'some_class')  do %>
    ...
<% end if ... %>
Salil
  • 43,381
  • 19
  • 113
  • 148