本文介绍了Money-Rails Gem:如何为所有货币制作选择列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 money-rails gem 并想在我的视图中显示不同货币的列表,但我现在拥有的代码不起作用.

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.

我有我的 Price 模型以及 in_centscurrency 字段:

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

现在根据 Money gem 和 Money-Rails 文档,我必须执行以下操作:

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

比我对简单形式 gem 的看法:

Than my view with simple form gem:

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

但这给了我错误:

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

为什么?

附言

我想显示 ISO 代码和名称,例如 United States Dollar (USD).

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

推荐答案

你的 all_currencies 方法是一个实例方法,你不是在实例上调用它.

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

添加self.all_currencies,然后用Price.all_currencies

希望能帮到你

这篇关于Money-Rails Gem:如何为所有货币制作选择列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 08:55