我正在尝试制作简单的身份验证/注册 ruby 应用程序。我正在使用 BCrypt gem 进行加密,现在它给我带来了一些问题。当我按下提交按钮时,它会抛出
undefined method `password_salt=' for #<User:0x007f93c36bc570>
好的,所以我认为这段代码应该是从模型到控制器的地方,但这给了我这个 er
undefined local variable or method `encrypt_password' for #<User:0x007f93c5f35f10>
我已经尝试过 rake db:migrate 和重新启动应用程序也 git repo:https ://github.com/pumpurs/auth
(我正在谈论的代码)
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
控制器:
class UsersController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(params[:user].permit(:password, :email, :password_confirmation))
if @user.save
redirect_to root_url, :notice => "Signed up!"
else
render "new"
end
end
private
def user_params
params.require(:user).permit(:password, :email, :password_confirmation)
end
end
模型文件:
class User < ActiveRecord::Base
attr_accessor :password, :salt
before_save :encrypt_password
validates_confirmation_of :password
validates_presence_of :password, :on => :create
validates_presence_of :email
validates_uniqueness_of :email
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
end