Ruby on Rails SQLite3 :: ConstraintException:NOT NULL约束失败:

时间:2015-12-12 11:51:01

标签: ruby-on-rails ruby sqlite

我正在开发一个简单的应用程序,用户可以将主题添加到购物车。在我添加身份验证之前,我能够将主题添加到购物车,但由于我希望用户能够访问他/她的购物车,我使用Devise来创建具有身份验证的用户。现在,当我点击按钮将主题添加到购物车时,我有以下错误:

这是我收到的错误的快照:1

  

SQLite3 :: ConstraintException:NOT NULL约束失败:carts.user_id:INSERT INTO" carts" (" created_at"," updated_at")VALUES(?,?)

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  private

    def current_cart
      Cart.find(params[:user_id])
    rescue ActiveRecord::RecordNotFound
      cart = Cart.create
      params[:user_id] = cart.id
      cart
    end
end


    class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_one :cart
end

class Cart < ActiveRecord::Base
  belongs_to :user
  has_many :line_items, dependent: :destroy

  scope :user_carts, ->(user) { where(['user_id= ?', user.id]) }

end

class AddUsersToCarts < ActiveRecord::Migration

  def up
    add_reference :carts, :user, index: true
    Cart.reset_column_information
    user = User.first

    Cart.all.each do |cart|
      cart.user_id = user.id
      cart.save!
    end

    change_column_null :carts, :user_id, false
    add_foreign_key :carts, :users
  end

  def down
    remove_foreign_key :carts, :users
    remove_reference :carts, :user, index: true
  end

end

编辑:我在下面添加了schema.rb:

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

  create_table "carts", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id",    null: false
  end

  add_index "carts", ["user_id"], name: "index_carts_on_user_id"

  create_table "line_items", force: :cascade do |t|
    t.integer  "subject_id"
    t.integer  "cart_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "subjects", force: :cascade do |t|
    t.string   "title",       null: false
    t.string   "code",        null: false
    t.text     "description", null: false
    t.integer  "credits",     null: false
    t.string   "lecturer",    null: false
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  add_index "subjects", ["title"], name: "index_subjects_on_title", unique: true

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

1 个答案:

答案 0 :(得分:0)

current_cart方法没有多大意义。

  1. 您无法通过致电Cart.find(params[:user_id])找到用户的购物车,因为它会查找id(而不是user_id)的购物车。
  2. Cart.create失败,因为您没有提供所需的user_id(您的数据库迁移表明该字段不能为空)。
  3. 此外params[:user_id] = cart.id更改了参数哈希,但不更改新购物车。
  4. 将该方法更改为类似内容(使用find_or_create_by)并使用current_user.id代替params[:user_id]

    def current_cart
      Cart.find_or_create_by(user_id: current_user.id)
    end