好的,所以我将money-rails gem整合到我的基本计算应用中......
当我尝试现在运行时,我收到此错误:
ActiveRecord::StatementInvalid in TippiesController#create
以及它下面的这一行:
PG::NotNullViolation: ERROR: null value in column "tip" violates not-null constraint DETAIL: Failing row contains (37, null, null, 2017-03-10 07:47:36.152504, 2017-03-10 07:47:36.152504, 3300, USD, 10, USD). : INSERT INTO "tippies" ("created_at", "updated_at", "cost_cents", "tip_cents") VALUES ($1, $2, $3, $4) RETURNING "id""
...
所以,我认为这意味着它产生了这个因为我的模型规定validates :tip, presence: true
...所以我评论了它,但仍然收到此错误。
不确定如何排序。任何帮助搞清楚都会受到赞赏。
当前模式
class Tippy < ApplicationRecord
validates :tip_cents, presence: true
validates :cost_cents, presence: true
monetize :tip_cents
monetize :cost_cents
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_cents + (tip_cents * cost_cents)
end
end
当前控制器
class TippiesController < ApplicationController
before_action :set_tippy, only: [:show, :edit, :update, :destroy]
# GET /tippies
# GET /tippies.json
def index
@tippies = Tippy.all
end
# GET /tippies/1
# GET /tippies/1.json
def show
#@calculation_of_total_cost
end
# GET /tippies/new
def new
@tippy = Tippy.new
end
# GET /tippies/1/edit
def edit
end
# POST /tippies
# POST /tippies.json
def create
@tippy = Tippy.new(tippy_params)
respond_to do |format|
if @tippy.save
format.html { redirect_to @tippy, notice: 'Tippy was successfully created.' }
format.json { render :show, status: :created, location: @tippy }
else
format.html { render :new }
format.json { render json: @tippy.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tippies/1
# PATCH/PUT /tippies/1.json
def update
respond_to do |format|
if @tippy.update(tippy_params)
format.html { redirect_to @tippy, notice: 'Tippy was successfully updated.' }
format.json { render :show, status: :ok, location: @tippy }
else
format.html { render :edit }
format.json { render json: @tippy.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tippies/1
# DELETE /tippies/1.json
def destroy
@tippy.destroy
respond_to do |format|
format.html { redirect_to tippies_url, notice: 'Tippy was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_tippy
@tippy = Tippy.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def tippy_params
params.require(:tippy).permit(:tip, :cost, :tip_cents, :tip_currency, :cost_cents, :cost_currency)
end
end
迁移的获利文件
class MonetizeTippy < ActiveRecord::Migration[5.0]
def change
add_monetize :tippies, :cost
add_monetize :tippies, :tip
end
end
模式
ActiveRecord::Schema.define(version: 20170310070749) 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
t.integer "cost_cents", default: 0, null: false
t.string "cost_currency", default: "USD", null: false
t.integer "tip_cents", default: 0, null: false
t.string "tip_currency", default: "USD", null: false
end
end
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_cents %>
</td>
</tr>
<tr>
<td>
Tip You Picked:
</td>
<td>
<%= number_to_percentage(@tippy.tip_cents * 100, format: "%n%", precision: 0) %>
</td>
</tr>
<tr>
<td>
The Total Cost:
</td>
<td>
<%= humanized_money_with_symbol @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 %>
答案 0 :(得分:1)
此错误来自数据库,而不是Rails。
在添加了tip列的迁移中,它看起来像这样:
t.float :tip, null: false
这会转换为SQL架构中的NOT NULL
约束。
数据库约束和验证不能用于相同的目的:前者用于明确防止不正确的数据被持久化,而后者是通过自动添加的{{1}将错误传达给用户的简单方法对象。
要解决您的问题,您必须添加另一个迁移并再次使ActiveModel::Errors
可以为空(并且可能提供适当的默认值):
tip