user2815613
user2815613

Reputation: 71

Conditional Text from Helper

I have a set of repeating records that should contain a string that should contain a block of conditional text:

<% 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

Is there any way to create a helper method in the OrdersHelper "payment_type" that will help me avoid conditional statements in my views?

Upvotes: 1

Views: 97

Answers (1)

Nitin Jain
Nitin Jain

Reputation: 3083

yes create a method in helper for that

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

now in view

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

Upvotes: 4

Related Questions