2013-09-22 91 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