【问题标题】:Money-Rails Gem and Instance CurrenciesMoney-Rails Gem 和实例货币
【发布时间】:2015-05-03 05:31:19
【问题描述】:

我正在尝试在我的 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

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

谢谢

【问题讨论】:

  • 能否分享一下您的型号代码?

标签: ruby-on-rails money-rails


【解决方案1】:

步骤:

  1. 添加列类型money

    轨道 3

    add_money :table_name, :participation_cost

    Rails 4
    add_monetize :table_name, :participation_cost

    这将为您提供两列

    :participation_cost_pennies #pennies 如果是 GBP,centes 如果是 USD

    :participation_cost_currency

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

    monetize :participation_cost_pennies, with_model_currency: :participation_cost_currency

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

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

【讨论】:

    猜你喜欢
    • 2015-07-20
    • 2015-08-15
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多