来自Helper的条件文本

时间:2014-01-06 17:42:13

标签: ruby-on-rails ruby

我有一组重复记录,其中应包含一个应包含条件文本块的字符串:

<% if @order.payments.present? %>
  <% @order.payments.each do |p| %>
    <tr>
      <td>Payment</td>
      <td>There was a "MY CONDITIONAL TEXT" for <span><%= number_to_currency(p.try(:amount)) %></span></td>
    </tr>
  <% end %>
<% end %>

So "MY CONDITIONAL TEXT" options would be:
"Partial Payment" when p.amount < @order.value 
"Excessive Payment" when p.amount > @order.value
"Payment" when p.amount = @order.value

有没有办法在OrdersHelper“payment_type”中创建一个帮助方法来帮助我避免在我的视图中使用条件语句?

1 个答案:

答案 0 :(得分:4)

是在帮助器中为该

创建一个方法
def method_name(order_value, payment_amount)
  if payment_amount < order_value
    "Partial Payment"
  elsif payment_amount > order_value
    "Excessive Payment"
  elsif payment_amount = order_value
    "Payment"
  end
end

现在在视野中

<%= method_name(@order.value, p.amount)%>