Money Gem - 未定义的方法`each'代表nil:NilClass

时间:2017-05-18 13:20:52

标签: ruby-on-rails ruby ruby-on-rails-4 ruby-on-rails-4.2 currency

我正在尝试在我的机器中使用money gem,但是我遇到了错误。

我指的是这个链接 - https://github.com/RubyMoney/money/blob/master/README.md

Money.new(1000, "USD").exchange_to("EUR")
NoMethodError: undefined method `each' for nil:NilClass
    from /home/dev/new_hotspotting-backend/shared/bundle/ruby/2.3.0/gems/money-currencylayer-bank-0.5.3/lib/money/bank/currencylayer_bank.rb:93:in `update_rates'
    from /home/dev/new_hotspotting-backend/shared/bundle/ruby/2.3.0/gems/money-currencylayer-bank-0.5.3/lib/money/bank/currencylayer_bank.rb:142:in `expire_rates!'
    from /home/dev/new_hotspotting-backend/shared/bundle/ruby/2.3.0/gems/money-currencylayer-bank-0.5.3/lib/money/bank/currencylayer_bank.rb:108:in `get_rate'
    from /home/dev/new_hotspotting-backend/shared/bundle/ruby/2.3.0/gems/money-6.7.0/lib/money/bank/variable_exchange.rb:109:in `exchange_with'
    from /home/dev/new_hotspotting-backend/shared/bundle/ruby/2.3.0/gems/money-6.7.0/lib/money/money.rb:434:in `exchange_to'
    from (irb):17

我不明白为什么我收到这个错误。 我正在使用以下版本的金钱宝石

money (6.9.0, 6.7.0)
money-currencylayer-bank (0.5.4)

enter image description here

1 个答案:

答案 0 :(得分:2)

尝试:

对象需要一个人手动指定汇率

Money.add_rate("USD", "EUR", 0.5)
Money.us_dollar(100).exchange_to("EUR")

了解更多official GEM rapo

输出:

2.3.0 :009 >   
2.3.0 :010 >   require 'money'
 => true 
2.3.0 :011 > Money.add_rate("USD", "EUR", 0.5)
 => 0.5 
2.3.0 :012 > Money.us_dollar(100).exchange_to("EUR").currency
 => #<Money::Currency id: eur, priority: 2, symbol_first: true, thousands_separator: ., html_entity: &#x20AC;, decimal_mark: ,, name: Euro, symbol: €, subunit_to_unit: 100, exponent: 2, iso_code: EUR, iso_numeric: 978, subunit: Cent, smallest_denomination: 1> 
2.3.0 :013 > 
2.3.0 :014 >   
相关问题