0

我正在尝试使用gem money-rails但没有成功。

我住在巴西,这里的小数点是“,”,千位分隔符是“。”

因此,我在 money.rb 初始化程序中添加了以下代码:

MoneyRails.configure do |config|
    config.default_currency = :brl
    config.register_currency = {
        :id                  => :brl, 
        :priority            => 1,
        :iso_code            => "BRL",
        :name                => "Real",
        :symbol              => "R$",
        :symbol_first        => true,
        :subunit             => "Cent",
        :subunit_to_unit     => 100,
        :thousands_separator => ".",
        :decimal_mark        => ","
    }
end

在我的模型类“Produto”中:

class Produto < ApplicationRecord
  attr_accessor :nome, :preco
  monetize :preco_centavos
end

但是当我尝试在 Rails 控制台中使用它时,我得到了不同的行为:

irb(main):001:0> p = Produto.new
=> #<Produto id: nil, nome: nil, preco_centavos: 0, preco_currency: "BRL", created_at: nil, updated_at: nil>
irb(main):002:0> p.preco = 1000.to_money
=> #<Money fractional:100 currency:BRL>
irb(main):003:0> p.preco.format
=> "R$1,000.00"

当我期望“R$1.000,00”时,格式方法返回“R$1,000.00”。

有人已经通过了这个?

PS:对不起我的英语不好

4

1 回答 1

0

money-rails这是gem 代码中的一个错误(或至少是一个误导性功能) 。设置Money.use_i18n = false。一旦你这样做了,那么你就不需要config.register_currency为逗号/小数做,至少。 1000.to_money('BRL').format产量"R$1.000,00"

请参阅https://github.com/RubyMoney/money-rails/blob/4a35ae823843fba80c036e2332e80d6b9e06dcb5/lib/money-rails/active_model/validator.rb#L58了解为什么会这样。

于 2016-08-07T20:13:19.187 回答