红宝石货币兑换宝石有用吗? [英] Ruby currency exchange gems that work?
问题描述
这是基于之前已解决的问题.我需要为我的基于 ruby 的应用程序加载不同货币的销售价格.我最近使用 gem google_currency
根据 Google API 转换价格.最近在某个时候它停止工作,我不知道为什么.我尝试过以各种方式进行测试,但无法弄清楚问题是什么.
This is based on an earlier question that was resolved. I need to load sale prices for my ruby-based app in different currencies. I was recently using the gem google_currency
to convert the prices based on the Google API. At some point recently it stopped working and I have no idea why. I have tried testing in various ways but can't work out what the problem is.
我现在正在尝试使用具有良好文档的交换"gem,但是我使用的方法在运行时不会在视图文件中生成任何内容.
I am now trying to use the 'exchange' gem which has good documentation however the method I am using is not producing anything in the view files when running.
根据交换宝石,简单的转换应该是这样的:
According to the exchange gem the simple conversion should be something like:
def exchange4
puts 10.in(:eur).to(:usd)
end
但是它没有在 html 视图中加载任何内容.欢迎任何建议,包括其他工作宝石!
However it is not loading anything in the html view. Any suggestions including other working gems welcome!
目前这段代码似乎可以通过,但是现在 Action Controller 告诉我它不知道转换率:
Currently this code seems like it would pass however now Action Controller is telling me it doesn't know the conversion rates:
def exchange4(goods)
require 'money'
require 'money-rails'
exr = Money.new(1, goods.currency).exchange_to(buyer.currency)
puts exr
end
动作控制器给出的错误是:
The error Action Controller is giving is:
No conversion rate known for 'GBP' -> 'EUR'
很奇怪..
推荐答案
RubyMoney
组织有一个很好的选择来处理货币、金钱和兑换.我使用 money 并且确实有效.对于 Rails 集成,他们有 money-rails.
RubyMoney
organization has a very good options to deal with currencies, money and exchange. I use money and it really works. For Rails integration they have money-rails.
交换示例:
Money.us_dollar(100).exchange_to('EUR')
Money.new(100, 'USD').exchange_to('EUR')
您可以使用eu_central_bank
gem(与money
兼容)来提取所有汇率.示例用法(在 rails 控制台中):
You can use eu_central_bank
gem (compatible with money
) to extract all exchange rates. Example usage (in rails console):
>> bank = EuCentralBank.new
>> bank.update_rates # if bank.last_updated.blank? || bank.last_updated < 1.day.ago
>> Money.default_bank = bank
那么:
>> Money.new(1, 'GBP').exchange_to('EUR')
=> #<Money fractional:1 currency:EUR>
这篇关于红宝石货币兑换宝石有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!