rails 3.1多种货币

rma*_*002 2 ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

我必须在应用程序中添加更多货币,就像在这个网络应用程序http://www.designconnected.com/上 一样,你可以看到它以你选择的任何货币转换价格并保持这种方式.我一直在寻找已经过时的宝石,教程找不到任何东西,在stackoverflow中有一些关于它的问题,但没有一个得到我需要的东西.

如果有人知道更好的宝石,最近发布了......请告诉我.或者如果没有它的宝石,我应该将currency_id添加到current_user,以便应用程序显示该用户的正确货币..但是我从哪里获取货币汇率...我一直在寻找一个解决方案3天了,什么都没有.

谢谢你的任何建议..

这个网址已被检查:

/sf/ask/95760731/

Rails 3 - 多种货币

https://github.com/RubyMoney/money

最后一个与https://github.com/RubyMoney/google_currency结合使用看起来就像我需要的那个......但是现在是获得如何使用它的教程的最佳时机.

如果无法找到/获得有关此内容的完整教程,请帮助了解如何启动它的一些想法.谢谢.

rma*_*002 7

https://github.com/RubyMoney/money-railshttps://github.com/RubyMoney/google_currency是要走的路.这不是我的问题或问题,但无论如何,这是我现在最接近的答案.这是我为实现这一目标所做的一些步骤:

gem文件中:

gem "json" #if you don't have it
gem "money"
gem "google_currency"
Run Code Online (Sandbox Code Playgroud)

创建文件money.rbconfig/initializers

require 'money'
require 'money/bank/google_currency'
require 'json'
MultiJson.engine = :json_gem
Money.default_bank = Money::Bank::GoogleCurrency.new
Run Code Online (Sandbox Code Playgroud)

product.rb(或任何需要转换价格的型号)

composed_of :price,
    :class_name => "Money",
    :mapping => [%w(price price), %w(currency currency_as_string)],
    :constructor => Proc.new { |price, currency| Money.new(price || 0, currency || Money.default_currency) },
    :converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
Run Code Online (Sandbox Code Playgroud)

并在视图文件中:

<%= number_to_currency(product.price.exchange_to(:EUR)) %>
Run Code Online (Sandbox Code Playgroud)

例如,我有一个IT语言环境(意大利语) - 意大利货币现在是欧元:

您将以欧元转换价格..它对我来说真的很好,money gem使用Google_currency转换来自USD的欧元价格,并且locale yml文件更改此区域设置的Currency,因此您的价格看起来像XXX,XX欧元而不是$ XXX,XX.

要为每个区域设置显示正确的货币,您需要添加:

it:
  number:
    currency:
      format:
        format: "%n %u"
        unit: "EUR"
Run Code Online (Sandbox Code Playgroud)

it.yml文件或其他语言,你有货币该国.