Search code examples
ruby-on-railsrubyruby-on-rails-4money-rails

Money-Rails Gem: How to make a select list for all currencies?


I'm using the money-rails gem and want to show in my view a list of different currencies but the code I have now isn't working.

I have my Price model and the fields in_cents and currency:

create_table :prices do |t|
  t.integer :in_cents, default: 0, null: false
  t.string :currency, default: 'USD', null: false

Now according to the Money gem and Money-Rails docs I had to do something like:

class Price < ActiveRecord::Base

  monetize :in_cents, as: "amount", with_model_currency: :in_cents_currency

  def all_currencies(hash)
    hash.keys
  end

Than my view with simple form gem:

= f.input :currency, collection: all_currencies(Money::Currency.table)
= f.input :amount, required: false

But this gives me the error:

undefined method `all_currencies' for #<#<Class:0xd154124>:0xd15bab4>

Why?

P.S.

I want to show the ISO Code and the name like United States Dollar (USD).


Solution

  • Your all_currencies method is an instance method, and you're not calling it on an instance.

    Add self.all_currencies, and then call it with Price.all_currencies

    Hope this helps