gpt4 book ai didi

ruby-on-rails - Rails 不保存模型字段

转载 作者:太空宇宙 更新时间:2023-11-03 17:50:50 25 4
gpt4 key购买 nike

我想知道为什么我的 Rails 没有保存我的 encrypted_pa​​ssword 字段。

这是我的用户 Controller

class UserController < ApplicationController
require 'bcrypt'
before_filter :save_login_state, :only => [:new, :create]
def new
new_user = User.new(user_params)
new_user.numRatings = 0
if new_user.save
flash[:notice] = "You signed up successfully"
flash[:color]= "valid"
else
flash[:notice] = "Form is invalid"
flash[:color]= "invalid"
end
redirect_to(:controller => 'sessions', :action => 'login')
end

def create
end

def update
end

def view
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end

这是我的用户模型

class User < ActiveRecord::Base
require 'BCrypt'
attr_accessor :password, :encrypted_password
EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :name, :presence => true
validates :password, :confirmation => true, :presence => true, :on => :create

before_save :encrypt_password
after_save :clear_password

def encrypt_password
if password.present?
self.salt = BCrypt::Engine.generate_salt
self.encrypted_password = BCrypt::Engine.hash_secret(password, salt)
end
end

def clear_password
self.password = nil
end

def self.authenticate(username_or_email="", login_password="")
if EMAIL_REGEX.match(username_or_email)
user = User.find_by_email(username_or_email)
end
if user && user.match_password(login_password)
return user
else
return false
end
end

def match_password(login_password="")
encrypted_password == BCrypt::Engine.hash_secret(login_password, salt)
end

end

另外,我用这个函数来保存

  def login_attempt
authorized_user = User.authenticate(params["user"]["email"],params["user"]["password"])
if authorized_user
session[:user_id] = authorized_user.id
flash[:notice] = "Wow Welcome again, you logged in as #{authorized_user.username}"
redirect_to(:action => 'home')
else
flash[:notice] = "Invalid Username or Password"
flash[:color]= "invalid"
render "login"
end
end

有一件事我怀疑我创建了我的第一个迁移

class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email
t.string :password
t.string :name
t.float :rating
t.integer :numRatings

t.timestamps
end
end
end

但是,我将 :password 字段更改为 :encrypted_pa​​ssword,它反射(reflect)在表中。我已经坚持了2个小时。我想知道是否有什么突出的地方。谢谢。

日志说正在注入(inject)数据减去加密的密码

插入“用户”(“created_at”、“email”、“name”、“numRatings”、“salt”、“updated_at”)值(?、?、?、?、?、?)[["created_at", "2014-08-05 06:22:41.335373"], ["email", "w@w.com"], ["name", "w"], ["numRatings", 0], [ "盐", "$2a$10$pagmnNzT4FWEBmaPmiLX1u"], ["updated_at", "2014-08-05 06:22:41.335373"]]

最佳答案

你的 attr_accessor :encrypted_pa​​ssword 很突出——这是用一个简单地在你的模型中设置一个名为 @encrypted_pa​​ssword 的实例变量的方法覆盖 Rails 生成的属性 getter/setter实例。

关于ruby-on-rails - Rails 不保存模型字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25132289/

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