ruby-on-rails - Devise::RegistrationsController#create 中的 ActiveRecord::StatementInvalid

标签 ruby-on-rails ruby sqlite

我正在尝试向我的 Devise 用户模型添加一些额外信息,例如名字、姓氏、年龄、性别和城市。

当我填写注册表并单击提交时,出现此错误:

SQLite3::ConstraintException:NOT NULL 约束失败:users.first_name:INSERT INTO "users"("email", "encrypted_pa​​ssword", "created_at", "updated_at") VALUES (?, ?, ?, ?)

def each
  loop do
    val = step
    break self if done?
    yield val
  end

这些是参数(如果有的话):

参数:

{"utf8"=>"✓", “authenticity_token”=>“2e5oUwMw84HtwSuI09X1O5kjPLYk7SW4VKgGOOxcB93W7sSQYjPgq3N/BGo0+oAEifhec4lQ3PUt9vub17vs7g==”, “用户”=> {"first_name"=>"测试", "last_name"=>"测试", "年龄"=>"69", "城市"=>"纽约", "性别"=>"变性人", "电子邮件"=>"[email protected] ", "密码"=>"[已过滤]", "password_confirmation"=>"[已过滤]"}, “提交”=>“注册”}

这是我的 schema.rb 以防万一:

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.string   "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.string   "unconfirmed_email"
    t.string   "first_name",                          null: false
    t.string   "last_name",                           null: false
    t.integer  "age",                                 null: false
    t.string   "city",                                null: false
    t.string   "gender",                              null: false
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

这也是我的 user.rb 模型:

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

最佳答案

为了在设备中尚未设置用户的附加列,我允许通过这种方式在我的 ApplicationContoller 中进行访问。 rails 4

before_filter :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters
  # Only add some parameters
  devise_parameter_sanitizer.for(:accept_invitation).concat [:first_name, :last_name]
end

before_action :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) << :full_name
end

这将允许您将名字与您设置的其他内容一起使用。

Rails5

def configure_permitted_parameters
  additional_params = [:name, :company, :email_confirmation,   {addresses_attributes: [:address1, :address2, :city, :state, :zip, :country, :name]}]
  devise_parameter_sanitizer.permit(:sign_up, keys: additional_params)
  devise_parameter_sanitizer.permit(:account_update, keys: additional_params)
end

关于ruby-on-rails - Devise::RegistrationsController#create 中的 ActiveRecord::StatementInvalid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42774336/

相关文章:

mysql - ActiveRecord 迁移失败并显示 "version is defined by Active Record"

ruby - 正则表达式从链接 URL 获取 ID

ruby - 使用 ruby​​-libvirt 扩展编译 Mountain Lion 时出现问题

android - 没有这样的表 SQLite Android

wcf - 单用户数据库与多用户数据库

ruby-on-rails - RecordNotFound与accepts_nested_attributes_for和belongs_to

html - 如何使用 gem htmltoword 在 Rails 中放置页眉和页脚?

c++ - 我是 RUBY 的新手,我需要了解 3 个函数

android - 如何在android中查看表格列表

ruby-on-rails - 应该将 gems 与该应用程序 bundle 在一起吗?