Interpret interpolated html tag as html, not string literal?

I’m trying have the HTML tag <br> interpreted as a line break, but instead it displays as a string literal in the view when I attempt this:

<%= property.address_line_2 + "<br>" if property.address_line_2.present? %>

I tried raw() and .html_safe but they the same effect.

<%= property.address_line_2 + raw("<br>") if property.address_line_2.present? %>

<%= property.address_line_2 + "<br>".html_safe if property.address_line_2.present? %>

Is there an elegant/idiomatic way of doing this, or is the best way to use another line? I figure this approach is just not so DRY:

<%= property.address_line_2 if property.address_line_2.present? %>
<% if property.address_line_2.present? %><br><% end %>

>Solution :

I would use

<% if property.address_line_2.present? %>
  <%= property.address_line_2 %><br>
<% end %>

This also has the added benefit of being a bit easier to read

Leave a Reply