2013-09-22 90 views
0

我想做简单的身份验证/注册红宝石应用程序。我使用BCrypt宝石进行加密,现在它给了我一些问题。当我按下提交按钮,它抛出验证失败后提交 - 导轨4

undefined method `password_salt=' for #<User:0x007f93c36bc570> 

好了,我红认为,这个代码应该是从模型到coontroler的地方,但是这给了我这个呃

undefined local variable or method `encrypt_password' for #<User:0x007f93c5f35f10> 

我已经试过耙分贝:迁移并重新启动应用程序还 混帐回购协议:https://github.com/pumpurs/auth

(代码IM谈论)

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 
+0

你是否试图将实际密码保存到数据库或加密密码? – tihom

回答

1

添加

attr_accessor :password_salt 

模型?

为什么不使用Devise或其他验证宝石?

+0

yess,所以mutch谢谢:) – andris

+0

试图从0学到全部,之后我会用 – andris