2017-02-21 4 views
1

사용자 모델과 컨트롤러가 있는데 사용자는 password, password_confirmationpassword_digest 필드입니다. 나는 bcrypt 루비 보석을 사용했습니다.레일 - 사용자의 비밀번호를 저장할 수 없습니다. bcrypt

사용자를 만들 때 위의 모든 필드를 입력하면 사용자가 생성됩니다. 그러나 사용자 password은 저장되지 않으므로 password_digest 필드에 16 진수 형식으로 저장됩니다.

사용자 이름 만 편집하고 사용자 편집 양식을 열면 양식에 passwordpassword_confirmation 필드가 비어 있습니다. 나는 원하지 않는 사용자를 저장하기 위해 새로운 비밀번호를 다시 부여해야합니다.

attr_accessor 도움이되지 않았습니다. 여기

내 사용자의 컨트롤러 :

before_filter :authorize 

    def create 
    @user = User.new(user_params) 
    if @user.valid? 
    @user.password = params[:user][:password] 
    @user.password_confirmation = params[:user][:password_confirmation] 
    @user.save! 
    redirect_to users_path 
    else 
    flash[:error] = @user.errors.full_message 
    render :new 
    end 
    end 

    def edit 
    @user = User.find(params[:id]) 
    end 

    def update 
    @user = User.find(params[:id]) 
    if @user.update_attributes(user_params) 
    redirect_to user_path 
    else 
    render 'edit' 
    flash[:error] = @user.errors.full_messages 
    end 
    end 

    private 
    def user_params 
    params.require(:user).permit(:first_name, :last_name, :emp_id, :email, :password, :password_confirmation) 
    rescue 
    {} 
    end 

Heres는 내 사용자 모델 :

class User < ApplicationRecord 
rolify 
require 'bcrypt' 
has_secure_password 
# other field validations here.... 
validates :password, presence: true 
validates :password_confirmation, presence: true 
end 

그리고 편집 형태 :

<%#= other fields here..... %> 
<%= f.label :Password_for_this_portal %> 
<%= f.password_field :password, :class=>"form-control" %> 
<%= f.label :Confirm_passsword_for_this_portal %> 
<%= f.password_field :password_confirmation, :class=>"form-control" %> 
# ..... submit button here 

방법 NOT 편집에서 암호 변경에 대해 다시 물어 사용자 서식?

+0

그러나, 레일 4.x의에로 검증되는 password를 비활성화 할 수 있습니다 일하기로되어 있으면 데이터베이스에 암호를 저장하지 마십시오. 보안상의 이유로 다이제스트로 저장됩니다. – trueinViso

+0

실제 암호는 절대로 저장하지 않고 오직 다이제스트 만 저장합니다. 이 방법은 데이터베이스가 해킹 된 경우 해커가 다른 서비스 암호를 추론하여 사용자 암호를 복구 할 수 없습니다. – Maxence

답변

1

has_secure_passwordpasswordpassword_digest의 유효성을 검사하도록 설계되었습니다.

class User < ApplicationRecord 
    has_secure_password :validations => false 
end 

당신은 같은 단지 create에 검증을 수행 할 수 있습니다 : 그것은 방법

validates :password, presence: true, :on => :create 
validates :password_confirmation, presence: true, :on => :create 
+0

감사 Shannon, 내가 두 가지 방법에 대해 유효성을 검사하려는 경우 : "삭제"및 "작성", ": on"은 두 가지 방법으로는 작동하지 않습니다. – suhasa