【问题标题】:Confused About Money-Rails Gem对 Money-Rails 宝石感到困惑
【发布时间】:2017-07-31 08:34:04
【问题描述】:

我正在使用基本的计算器应用程序...

试图将money-gem融入其中但感到困惑......

这是当前架构(如您所见,它没有任何以 _cents 结尾的列......如果需要 _cents 部分,则非常困惑......

当前架构

ActiveRecord::Schema.define(version: 20170301051956) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "tippies", force: :cascade do |t|
    t.float    "tip",        null: false
    t.decimal  "cost",       null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

在模型中,我添加了monetize :tip & monetize: cost ...

我是否需要修改架构中的tip 和cost 列,即将其改成整数,并添加帖子_cents 部分?

当前模型

class Tippy < ApplicationRecord

    validates :tip, presence: true
    validates :cost, presence: true

    monetize :tip
    monetize :cost


    TIP_CHOICES = { "10%" => ".10", "20%" => ".20", "30%" => ".30", "40%" => ".40", "50%" => ".50", 
                    "60%" => ".60", "70%" => ".70", "80%" => ".80", "90%" => ".90" }


    def calculation_of_total_cost 
        cost + (tip * cost)
    end

end

当我尝试转到我的主页(设置为新页面)时,我收到此错误:

ArgumentError in TippiesController#new

Unable to infer the name of the monetizable attribute for 'tip'. Expected amount column postfix is '_cents'. Use :as option to explicitly specify the name or change the amount column postfix in the initializer.

特指模型中的monetize :tip...

Show.html.erb

<br/><br/>
<h1 class="text-center">Your Total Cost</h1>
<br/><br />


<table class="table table-striped">
    <tr>
        <td>
            Cost of Your Meal:
        </td>
        <td>
            <%= humanized_money_with_symbol @tippy.cost) %>
        </td>
    </tr>
    <tr>
        <td>
            Tip You Picked:
        </td>
        <td>
             <%= number_to_percentage(@tippy.tip * 100, format: "%n%", precision: 0) %>
        </td>
    </tr>
    <tr>
        <td>
            The Total Cost:
        </td>
        <td>
            <%= number_to_currency(@tippy.calculation_of_total_cost) %>
        </td>
    </tr>
</table>

new.html.erb

<br /><br />
<h1 class="text-center">Calculate Your Tip!</h1>

<%= render 'form', tippy: @tippy %>

_form.html.erb

<%= form_for(tippy, :html => {'class' => "form-horizontal"}) do |f| %>
  <% if tippy.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(tippy.errors.count, "error") %> prohibited this tippy from being saved:</h2>

      <ul>
      <% tippy.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>


  <div class="field form-group">
    <%= f.label :cost_of_your_meal, class: "control-label" %>
    <%= f.text_field :cost, class: "form-control" %>
  </div>

  <div class="field form-group">
    <%= f.label :pick_your_tip, class: "control-label" %>
    <%= f.select(:tip, Tippy::TIP_CHOICES, class: "form-control")  %>
  </div>


  <div class="actions">
    <%= f.submit class: "btn btn-primary btn-lg btn-block" %>
  </div>
<% end %>

我该如何进行这项工作?如果需要更多信息,请告诉我。谢谢

【问题讨论】:

  • 对所有货币列使用小数类型(不是浮点数)。
  • @max,所以,重命名所有列以添加 _cents 并使其浮动?
  • money-rails 有自己的迁移方法 - read the docs
  • @max,伙计,我确实阅读了文档。我仍然很困惑。我很高兴你明白了。我没有。因此,我为什么要问。诚实地。如果我想通了,我就不会问了。
  • 长解释是有两种存储货币的方法 - 十进制或最低面额的整数。您正在使用一个浮点列,这将导致舍入错误。使用 gem 提供的迁移方法将为您提供正确的名称和类型。

标签: ruby-on-rails ruby money-rails


【解决方案1】:

起初我也对此感到困惑。 gem 希望您使用它提供的第一列,即添加了_cents 的列的名称。所以你的模型应该有这个

monetize :tip_cents
monetize :cost_cents

该列和带有_currency 的列是由gem 创建的。

【讨论】:

  • 这特别令人困惑,因为迁移助手中的语法是例如t.monetize :tip 但在 model 中是 monetize :tip_cents ? - 它知道一个地方的后缀,但不知道另一个地方。
  • 我完全同意,起初这让我很困惑
猜你喜欢
  • 1970-01-01
  • 2013-01-25
  • 2011-08-01
  • 2012-05-21
  • 2014-07-08
  • 2017-02-28
  • 2013-07-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多