if rails 美元金额的条件

if condition for amount in USD $ in rails

我的要求是仅当我的@order.display_total < $200 时才显示按钮 所以当我包含给定代码时:

- if method.method_type == 'cashondelivery' && @order.display_total < 0
     .form-buttons{"data-hook" => "buttons"}
        = form.hidden_field :cod_pay, :value => true
        = submit_tag "Order Now", :class => 'order-now btn btn-danger'

它给我错误:

NoMethodError - undefined method `<' for #<Spree::Money:0x007ff3d9366490>:

@order 在哪里获取这个:

#<Spree::Order id: 5964, number: "R981938713", item_total: #<BigDecimal:7ff3d2514f78,'0.3843E3',18(18)>, total: #<BigDecimal:7ff3cea3bd20,'0.3843E3',18(18)>, state: "address", adjustment_total: #<BigDecimal:7ff3d25149b0,'0.0',9(18)>, user_id: 1, completed_at: nil, bill_address_id: 24481, ship_address_id: 24482, payment_total: #<BigDecimal:7ff3d25142f8,'0.0',9(18)>, shipping_method_id: nil, shipment_state: nil, payment_state: nil, email: "admin@skinnymint.com", special_instructions: nil, created_at: "2015-12-27 03:45:02", updated_at: "2015-12-28 12:30:34", currency: "USD", last_ip_address: "127.0.0.1", created_by_id: 1, shipment_total: #<BigDecimal:7ff3d251eb68,'0.0',9(18)>, additional_tax_total: #<BigDecimal:7ff3d251ea00,'0.0',9(18)>, promo_total: #<BigDecimal:7ff3d251e7d0,'0.0',9(18)>, channel: "spree", included_tax_total: #<BigDecimal:7ff3d251e5c8,'0.0',9(18)>, item_count: 7, approver_id: nil, approved_at: nil, confirmation_delivered: false, considered_risky: false, guest_token: "aGoCAkyLXJs1oOUp9dS96w", locale: nil, state_lock_version: 0, cod_pay: false>

和@order.display_total = 398.40 美元

请指导我如何设置 if 条件,因为我是新手 rails。提前致谢。

不要使用 $。如果 order.display_total 是一个字符串:转换为 int/double/float 然后尝试用 < 200

测试它

你的条件是比较不同的类型。小于号只能用来比较数字。您需要将 @order.display_total 转换为数字并从比较的右侧删除美元符号。

更好的办法是将该逻辑移至辅助方法,从而使视图不受逻辑影响。

我建议在不带“$”的情况下比较金额 我不确定您如何存储金额,因此如果您可以直接比较数字,这是最容易使用的解决方案。 否则创建一个简单的方法来提取数字并比较它们:

def is_allowed?(amount,total)
  nbr1 = amount[1..-1]
  nbr2 = total[1..-1]
  amount < total
end

@order.display_total returns 没有比较的 "money" 对象。但是我注意到您的对象还有一个 "item_total" 字段,该字段似乎具有数值。所以 @order.item_total < 200 会起作用。但是,这不会考虑货币转换等。