link_to中的Ruby三元组

Bit*_*ise 1 ruby refactoring ruby-on-rails erb

我的基本问题是如何使用三元组将其转换为一行代码?

 <% 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 %>
Run Code Online (Sandbox Code Playgroud)

我正在做的就是在特定条件下更改类名.

Urs*_*sus 5

我认为它在两行中更具可读性

<% link_class = tip_off ? "topbar-brand-tip-off" : "topbar-brand" %>
<%= link_to "Dead Man's Snitch", [:homepage], class: link_class, rel: "home" %>
Run Code Online (Sandbox Code Playgroud)