1

Right now when I use

<%= @inbox.automatic_reconciliation ? "<i class='fi-play-circle'></i>" : "<i class='fi-pause'></i>" %>

My view spits out the actual code on the page instead of the icon. I've tried using a few methods like .to_html and such to no avail - what am I missing?

Evan Lemmons
  • 807
  • 3
  • 11
  • 27

3 Answers3

0

Try using the html_safe method.

<%= @inbox.automatic_reconciliation ? "<i class='fi-play-circle'></i>".html_safe : "<i class='fi-pause'></i>".html_safe %>

documentation

Kleber S.
  • 8,110
  • 6
  • 43
  • 69
0

If you insist on doing this, use the raw helper, which is preferred over html_safe:

<%= raw @inbox.automatic_reconciliation ? "<i class='fi-play-circle'></i>" : "<i class='fi-pause'></i>" %>

The better solution by far is to remove this logic from your views. Create a helper method and simply invoke it:

<i class="<%= automatic_reconciliation_class %>"></i>
user229044
  • 232,980
  • 40
  • 330
  • 338
  • Why is preferred? Except an error raise on `nil`. Both output is a string. – Roman Kiselenko May 10 '16 at 21:22
  • @Зелёный It is shorter, clearer, less easy to miss and doesn't raise exceptions when the input is `nil`. No, there is no chance of a `nil` input in this case, but the point is that you should standardize on one method, and `raw` is better in the general case. `raw` is a helper method specifically provided for use in your views for exactly this case. Rails is a land of conventions, and you should use `raw` for that reason if no other. – user229044 May 10 '16 at 21:23
0

You can use the ternary operator to make it simple:

<i class="<%= @inbox.automatic_reconciliation ? "fi-play-circle" : "fi-pause" %>"></i>

This embeds the condition directly around the class name, which cleans up the code quite a lot. Only the name of the class can possibly be affected, so the rest of the HTML is isolated from incidental changes that could lead to maintenance issues in the future.

Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43