首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >对金钱感到困惑--Rails Gem

对金钱感到困惑--Rails Gem
EN

Stack Overflow用户
提问于 2017-03-10 05:28:29
回答 1查看 1.1K关注 0票数 1

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

试着把钱-宝石融入其中但是很困惑.

这是当前的模式(如您所见,它没有以_cents结尾的任何列.如果需要_cents部件的话.

当前模式

代码语言:javascript
复制
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

是否需要修改模式中的tipcost列,即将其重新设置为整数,并添加post _cents部分?

电流模型

代码语言:javascript
复制
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

代码语言:javascript
复制
<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

代码语言:javascript
复制
<br /><br />
<h1 class="text-center">Calculate Your Tip!</h1>

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

_form.html.erb

代码语言:javascript
复制
<%= 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 %>

我该怎么做呢?如果需要更多的信息,请告诉我。谢谢

EN

回答 1

Stack Overflow用户

发布于 2017-12-31 22:25:28

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

代码语言:javascript
复制
monetize :tip_cents
monetize :cost_cents

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42710909

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档