问题
I have to add more currencies to the app like on this web app http://www.designconnected.com/ as you can see, it converts the price in whatever currency you select and keep it this way. I've been looking for gems that are out of date already, tutorials couldn't find any and in stackoverflow there are a few questions about it but none of them got what I need.
if any of you know a better gem, recently released... please let me know. or if there is no gem for it, should I add a currency_id to current_user so the app will show the proper currency for this user.. but then where do I take the currency rates from.. I've been looking for a solution for 3 days now and nothing.
Thank you for any given advice..
this urls have been checked:
https://stackoverflow.com/questions/1368010/rails-currency-gem-or-plugin
Rails 3 - Multiple Currencies
https://github.com/RubyMoney/money
the last one in combination with https://github.com/RubyMoney/google_currency looks like it's the one I need.. but now would be the right time to get a tutorial on how to use this.
Please help with some ideas on how to get it started if there is no way to find/get a full tutorial about this. Thank you.
回答1:
https://github.com/RubyMoney/money-rails and https://github.com/RubyMoney/google_currency is the way to go. It's not what I asked or in the question, but anyway it's the closest answer I have right now. These are a few steps I did to get this working:
In gem file:
gem "json" #if you don't have it
gem "money"
gem "google_currency"
Create a file money.rb in config/initializers
require 'money'
require 'money/bank/google_currency'
require 'json'
MultiJson.engine = :json_gem
Money.default_bank = Money::Bank::GoogleCurrency.new
In product.rb (or whatever model you have that needs the price to be converted)
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") }
And in the view file:
<%= number_to_currency(product.price.exchange_to(:EUR)) %>
For exemple, I have an IT locale (Italian language) - Italy currency now is Euro:
You'll have the prices converted in EUR.. It worked for me really nice, money gem convert the price from USD in EUR using Google_currency, and the locale yml file changes the Currency for this locale so you'll have the price looking like XXX,XX EUR and not $XXX,XX.
To display the right currency for each locale you need to add:
it:
number:
currency:
format:
format: "%n %u"
unit: "EUR"
In it.yml file or for other language you have with currency for that country.
回答2:
You probably don't need a gem for this. You can make a call directly to google's currency API at any point in your code using the URL's explained here. This could be done in your model or via AJAX directly in the view.
http://motyar.blogspot.com/2011/12/googles-currency-converter-and-json-api.html
来源:https://stackoverflow.com/questions/10199301/rails-3-1-multiple-currencies