gpt4 book ai didi

ruby-on-rails - Rails NoMethodError:未定义的方法`password_digest =

转载 作者:行者123 更新时间:2023-12-03 12:19:26 26 4
gpt4 key购买 nike

我是Rails的新手,已经看到了我所有问题的所有可能答案,因为其他开发人员经常问这个问题,但我无法解决它。请看一看。

当我尝试从控制台添加数据时出现此错误

User.create(name: "Michael Hartl", email: "mhartl@example.com", phone: "0123456789", password: "foobar", password_confirmation: "foobar")


错误显示

undefined method `password_digest=' for #<User:0x0000000375c788>
Did you mean? password=


控制者

 def create
@candidate = User.new(user_params)
if @candidate.save
flash[:notice] = "New Candidate Added Successfully"
redirect_to(users_path)
else
render('new')
end
end

private

def user_params
#Whitelisting for strng parameters
params.require(:user).permit(:name, :email, :password, :password_confirmation, :qualification, :college, :stream, :phone)
end


移民:

class CreateUsers < ActiveRecord::Migration[5.1]
def change
create_table :users do |t|
t.string :name, null: false
t.boolean :admin_user, default: false
t.string :email, null: false
t.string :password_digest, null: false
t.string :qualification
t.string :college
t.string :stream
t.string :phone
t.timestamps
end
end
end


模型

require 'bcrypt'
class User < ApplicationRecord
include BCrypt
has_many :results, dependent: :destroy
has_many :exams, through: :results
accepts_nested_attributes_for :results

VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

before_save { self.email = email.downcase }

has_secure_password

validates :email, presence: true
validates :email, presence: true, length: { maximum: 255 },format: { with: VALID_EMAIL_REGEX },uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates_confirmation_of :password
validates_presence_of :password_confirmation
validates :name, presence: true
validates :phone, numericality: {only_integer: true}, length: {is: 10 , message: "length should be 10"}

scope :visible, lambda { where(:visible => true) }
scope :invisible, lambda { where(:visible => false) }
scope :sorted, lambda { order("id ASC") }
scope :newest_first, lambda { order("created_at DESC") }
scope :search, lambda {|query| where(["name LIKE ?", "%#{query}%"]) }


end

最佳答案

您是否已检查此https://github.com/plataformatec/devise/issues/1845

根据问题中的描述,您正在将ActiveModel的has_secure_password与Devise一起使用。您不能将两者混在一起。

可能从用户模型中删除has_secure_password将解决您的问题。

关于ruby-on-rails - Rails NoMethodError:未定义的方法`password_digest =,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48138958/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com