0

我正在尝试在我的 rails 4 应用程序中使用 money-rails gem。

我有一个参与者模型,其中包含每种货币和参与成本的属性。我的目标是让用户为每个实例指定一种货币,以及参与成本的金额。

在我的参与者模型中,我有:

货币化:participation_cost,with_model_currency: :amount_currency

在我的表格中,我要求用户选择一种货币并指定如下金额:

<%= f.input :participation_cost, label: 'What amount will you pay for participation costs?', :label_html => { :class => 'question-participants' }, placeholder: 'Whole numbers only', :input_html => {:style=> 'width: 250px; margin-top: 20px',  class: 'response-participants'} %>

<br><br>
<%= f.input :currency, label: 'Select your costs currency', label_html: {class: 'fundingspace'}, collection: ["AUD Australian Dollars", "GBP British Pounds", "USD US Dollars" ], prompt: "Choose one" %> </div> <br>

在我看来,我想显示货币和金额。我目前有一个字符串如下:

  <%=  "#{@project.scope.try(:participant).try(:participation_cost)} #{@project.scope.try(:participant).try(:currency)}" %>

当我对此进行测试时,我只得到了参与成本的数字。我不明白货币。

在我的 money.rb 初始化程序中,我有:

 config.default_currency = :gbp

任何人都可以帮忙吗?我不知道如何使用这个宝石。我已经按照用户指南进行操作,但它只能设置模型,例如基于实例的货币选择。有没有人成功地将它用于此目的?

谢谢

4

1 回答 1

5

脚步:

  1. 添加列类型钱

    导轨 3

    add_money :table_name, :participation_cost

    导轨 4
    add_monetize :table_name, :participation_cost

    这将为您提供两列

    :participation_cost_pennies #pennies 如果 GBP,cent 如果 USD

    :participation_cost_currency

  2. 在模型中以下行覆盖此列的默认和全局货币,并将在此列中使用货币

    monetize :participation_cost_pennies, with_model_currency: :participation_cost_currency

  3. 只需更新表单中的字段并保存货币,就像您保存但在participation_cost_currency 列中一样。

如果您不想在货币列中添加便士或参与成本,您可以在 Money.rb 中进行修改。看这里

于 2015-05-07T15:57:54.837 回答