Money-Rails Gem和Instance Currencies

Mel*_*Mel 0 ruby-on-rails money-rails

我试图在我的rails 4 app中使用money-rails gem.

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

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

货币化: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>
Run Code Online (Sandbox Code Playgroud)

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

  <%=  "#{@project.scope.try(:participant).try(:participation_cost)} #{@project.scope.try(:participant).try(:currency)}" %>
Run Code Online (Sandbox Code Playgroud)

当我测试这个时,我只得到参与号码的数字.我没有得到货币.

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

 config.default_currency = :gbp
Run Code Online (Sandbox Code Playgroud)

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

谢谢

Par*_*gra 5

脚步:

  1. 添加列类型的钱

    Rails 3

    add_money :table_name, :participation_cost

    Rails 4
    add_monetize :table_name, :participation_cost

    这将为您提供两列

    :participation_cost_pennies #pennies如果GBP,美分如果美元

    :participation_cost_currency

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

    monetize :participation_cost_pennies, with_model_currency: :participation_cost_currency

  3. 只需在表格中更新您的字段,并在保存时保存货币,但在participant_cost_currency列中.

如果您不想在结束时使用便士或在货币列中使用participant_cost,则可以在Money.rb中修改它.看到这里