Cameron Bass Cameron Bass - 1 month ago 9
Ruby Question

Ruby ternary in a link_to

My basic question is how can I turn this to one line of code using a ternary?

<% if tip_off %>
<%= link_to "Dead Man's Snitch", [:homepage], class: "topbar-brand-tip-off", rel: "home" %>
<% else %>
<%= link_to "Dead Man's Snitch", [:homepage], class: "topbar-brand", rel: "home" %>
<% end %>


All I'm doing is changing the class name if a particular condition.

Answer

It's more readable in two lines I think

<% link_class = tip_off ? "topbar-brand-tip-off" : "topbar-brand" %>
<%= link_to "Dead Man's Snitch", [:homepage], class: link_class, rel: "home" %>