我使用Money gem来处理交易金额。我想为不同的交易使用不同的货币(但没有转换)。默认货币在money.rb中设置:
config.default_currency = :usd
即使我可以在创建交易时设置不同的货币,视图也始终以美元显示金额。例如,以RUB作为货币的这个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"
有关如何覆盖默认值的任何想法?我会感激任何建议。
答案 0 :(得分:2)
使用amount而不是amount_cents解决了问题。