【问题标题】:Rails money gem - impossible to override default currencyRails money gem - 无法覆盖默认货币
【发布时间】:2013-12-16 15:46:12
【问题描述】:

我正在使用 Money gem 来处理交易金额。我想为不同的交易使用不同的货币(但没有转换)。在money.rb中设置了默认货币:

config.default_currency = :usd

尽管我可以在创建交易时设置不同的货币,但视图始终以美元显示金额。例如这个 12.00 以卢布为货币的交易:

<Transaction id: 100, amount_cents: 1200, currency: "RUB", created_at: "2013-12-11 09:32:52", updated_at: "2013-12-11 09:32:52"> 

在我看来,它显示为美元交易。

<% transactions.each do |transaction| %>
    <%= transaction.amount_cents.to_money.format %>
...

=> $12.00

这是我的 Transaction.rb 中的代码(以防我遗漏了什么)

  composed_of :amount_cents,
              class_name: 'Money',
              mapping: %w(amount_cents cents),
              converter: Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : Money.empty }              

  monetize :amount_cents, as: "amount"  

关于如何覆盖默认值的任何想法?如有任何建议,我将不胜感激。

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    使用 amount 而不是 amount_cents 解决了问题。

    【讨论】:

      猜你喜欢
      • 2016-08-23
      • 2015-07-20
      • 1970-01-01
      • 2016-12-07
      • 1970-01-01
      • 2015-08-15
      • 2014-07-22
      • 2013-02-17
      • 1970-01-01
      相关资源
      最近更新 更多