Ruby ternary operator in erb?

Reed G. Law picture Reed G. Law · Mar 9, 2011 · Viewed 14.5k times · Source

How can I make this code look better:

<%=raw manuscript.uploaded_to_s3? ? "<span style=\"color:green;\">" : "<span style=\"color:red;\">" %>

That is, can the HTML go outside of the ERB block making this easier to read?

Answer

Reuben Mallaby picture Reuben Mallaby · Mar 9, 2011
<span style="color:<%= manuscript.uploaded_to_s3? ? 'green' : 'red' %>">

I would advocate a CSS class rather than style attribute 8P:

<span class="<%= manuscript.uploaded_to_s3? ? 'green' : 'red' %>">