1

I've been searching for hours and viewed all google results one by one... Read about RubyMoney, Money-Rails. I'm new to rails and I still can't understand how to get a currency switcher for my whole Ruby on Rails app.

  1. In my Gemfile

    gem 'money-rails' 
    
    gem 'json'
    
    gem 'bigdecimal'
    
  1. bundle
  2. Created config/initializers/money.rb

    MoneyRails.configure do |config|
      config.default_currency = :eur
    end
    
  3. <%= humanized_money_with_symbol (item.item_price) %> formats that number as a currency in the view.

  4. How do I get a currency selector like Airbnb for instance to change all the prices of the website with automatic rate update from EU bank or google? I can't understand the RubyMoney documentation regarding this...

Shelvacu
  • 4,245
  • 25
  • 44
  • You're talking about currency exchange factors, not just currency presentation issues. Sites like that download exchange rates from somewhere and save that in a table used to convert when displaying values. – tadman Apr 18 '16 at 17:04
  • Thanks. Any idea on what information I should search for to get started? Can it be done only with Money-Rails or is it much more complex? –  Apr 18 '16 at 17:25
  • MoneyRails does provide to ability to convert exchange rates through Banks [See here](https://github.com/RubyMoney/money#exchange-rate-stores) for a better idea – engineersmnky Apr 18 '16 at 17:54
  • In other Gits it was quite easy to follow the instructions but here I don't know where should I add those lines? In a newly created models/money.rb or in config/initializers/money.rb ? Then they say "Stores must implement the following interface: " - I don't know neither how to use that. Just knowing the location where to add these could really unblock me in the comprehension of how it works. –  Apr 18 '16 at 18:07

1 Answers1

1

Here's where I would do as starting point.

1) Add to Gemfile:

gem 'google_currency'

2) Add to config/initializers/money.rb:

MoneyRails.configure do |config|
  config.default_currency = :eur

  # set default bank to instance of GoogleCurrency
  Money::Bank::GoogleCurrency.ttl_in_seconds = 86400
  config.default_bank = Money::Bank::GoogleCurrency.new
end

3) Add a currency selector somewhere in application layout or wherever you think it should go with all currencies you are interested in (:usd, :eur, etc.). Use either on change javascript event of this select box or a button to trigger a rails action that saves the selected currency on a session variable (let's call it from now on session[:currency]) and that also refreshes the current page (in case you are showing some prices on the current page - if you're using just a button a simple redirect_to :back would do otherwise if your response is in js format use window.location.reload();). Action would look something like this:

def save_currency
  session[:currency] = params[:currency]
  respond_to do |format|
    format.html { redirect_to :back }
  end
end

And on your routes.rb (replace controller for whatever you want) something like this:

post '/controller/save_currency', to: 'controller#save_currency'

Just the form with select box and submit button missing - do this as you please :)

4) Add a helper method to render prices (I'm assuming all your price attributes are defined as monetize using the money-rails gem). All prices that you want to show on your site should use this method to be rendered on a view. Method would look something like this (you can put it on your application_helper.rb if you don't see other place it would fit better):

def converted_price(price)
  if session[:currency].present?
    humanized_money_with_symbol(price.exchange_to(session[:currency]))
  else
    humanized_money_with_symbol(price)
  end
end

This is a simplified version, you should also add some exception handling look at google_currency gem for more documentation on this.

5) On all the views where you want to print a price, use a code similar to this (I'm using the same example as you did):

<%= converted_price(item.item_price) %>

Hope it helps!

Rodrigo Martinez
  • 611
  • 5
  • 14