【发布时间】:2019-12-25 06:38:24
【问题描述】:
我正在开发一个简单的应用程序,用户可以在其中将主题添加到购物车。但是有一个关于非空约束的错误。
浏览器显示的错误信息是这样的。
SQLite3::ConstraintException: NOT NULL 约束失败:items.title: INSERT INTO "items" ("image", "created_at", "updated_at") VALUES (?, ?, ?)
我已尝试删除schema.rb 中的非空约束。但是错误信息仍然存在。那么,我该怎么办?
架构:
create_table "items", force: :cascade do |t|
t.string "image", null: false
t.string "title", null: false
t.string "description", null: false
t.string "stock", null: false
t.string "price", null: false
t.integer "status", limit: 1, default: 0, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
控制器:
class SellController < ApplicationController
def new
@item = Item.new
end
def confirm
@item = Item.new(title: params[:title],price: params[:price],stock: params[:stock],description: params[:description],image: "default_item.jpg")
render :new if @item.invalid?
end
def create
@item = Item.new(title: params[:title],price: params[:price],stock: params[:stock],description: params[:description],image: "default_item.jpg")
#@item = Item.new(item_params)
if params[:image]
@item.image = "#{@item.id}.jpg"
image = params[:image]
File.binwrite("public/item_images/#{@item.image}", image.read)
end
if params[:back]
format.html { render :new }
elsif @item.save
flash[:notice] = "your item data is saved."
redirect_to("/sell/complete")
else
render("sell/new")
end
end
def complete
end
end
我希望保存项目数据并将浏览器上的页面更改为感谢页面。
【问题讨论】:
-
尝试填写您的日期时间,created_at & updated_at
标签: ruby-on-rails ruby sqlite null ruby-on-rails-5