Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<%= 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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading