Money-Rails Gem: How to Make a Picklist for All Currencies?

I am using a gem with money rails and want to show in my opinion a list of different currencies, but the code I have is Working.

I have my model Price

and 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 the Money-Rails docs I needed 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 a simple gem form:

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

      

But this gives me an error:

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

      

Why?

PS

I want to show the ISO code and type name United States Dollar (USD)

.

+3


source to share


3 answers


Your all_currencies method is an instance method and you are not calling it on an instance.

Add self.all_currencies

and then call it withPrice.all_currencies



Hope it helps

+2


source


Not sure if this is the best solution, but I made a helper as such:



  def currency_codes
    currencies = []
    Money::Currency.table.values.each do |currency|
      currencies = currencies + [[currency[:name] + ' (' + currency[:iso_code] + ')', currency[:iso_code]]]
    end
    currencies
  end

      

+7


source


Simplest solution:

= f.select :currency, Money::Currency.table

      

0


source







All Articles