2014-11-05 4 views
1

내 dashboard_user 컨트롤러는 다음과 같습니다암호가 될 수 없습니다 빈

class DashboardUsersController < ApplicationController 
    before_action :set_dashboard_user, only: [:show, :edit, :update, :destroy] 

    # GET /dashboard_users 
    # GET /dashboard_users.json 
    def index 
    @dashboard_users = DashboardUser.all 
    end 

    # GET /dashboard_users/1 
    # GET /dashboard_users/1.json 
    def show 
    end 

    # GET /dashboard_users/new 
    def new 
    @dashboard_user = DashboardUser.new 
    end 

    # GET /dashboard_users/1/edit 
    def edit 
    end 

    # POST /dashboard_users 
    # POST /dashboard_users.json 
    def create 
    @dashboard_user = DashboardUser.new(dashboard_user_params) 
#@dashboard_user.password = @dashboard_user.encrypted_password 
    respond_to do |format| 
     if @dashboard_user.save 
     format.html { flash[:notice] = 'User successfully Created.' and redirect_to action: "index"} 
     else 
     format.html { render :new } 
     end 
    end 
    end 

    # PATCH/PUT /dashboard_users/1 
    # PATCH/PUT /dashboard_users/1.json 
    def update 
    respond_to do |format| 
     if @dashboard_user.update(dashboard_user_params) 
     format.html { flash[:notice] = 'User successfully Edited.' and redirect_to action: "index"} 
     else 
     format.html { render :edit } 
     end 
    end 
    end 

    # DELETE /dashboard_users/1 
    # DELETE /dashboard_users/1.json 
    def destroy 
    @dashboard_user.destroy 
    respond_to do |format| 
     format.html { redirect_to dashboard_users_url, notice: 'User successfully Deleted.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_dashboard_user 
     @dashboard_user = DashboardUser.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def dashboard_user_params 
     params.require(:dashboard_user).permit(:user_id, :username, :normalized_user_name, :encrypted_password, :last_name, :first_name, :middle_name, :phone, :email, :seq_ques_id, :seq_ques_answer, :expire_password_ind, :expire_password_date, :deactivated_ind, :deactivated_date, :role_id, :created_by, :updated_by) 
    end 
end 

내 dashboard_use 모델 내 form.html이

class DashboardUser < ActiveRecord::Base 

    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable,:rememberable, :trackable, :validatable 

#require 'digest' 
    #before_save :encrypt_password 
    #def encrypt_password 
    # require 'digest' 
    #self.password = Digest::SHA1.hexdigest(self.password) 
# end 



    attr_accessor :login 
    validates :username, presence: true, length: {maximum: 50 ,message: 'Exceeds Maximum number of Characters.'}, uniqueness: { case_sensitive: false }, format: { with: /\A[a-zA-Z0-9]*\z/, message: "may only contain letters and numbers." } 
    validates :encrypted_password, presence: {message: ' can''t be Blank!'}, length: {maximum: 50 , message: 'Exceeds Maximum number of Characters.'} 
    validates :last_name, presence: {message: 'can''t be Blank!'}, length: {maximum: 50, message: 'Exceeds Maximum number of Characters.'} 
    validates :first_name, presence: true, length: {maximum: 50, message: 'Exceeds Maximum number of Characters.'} 
    validates :middle_name, length: {maximum: 50 ,message: 'Exceeds Maximum number of Characters.'} 
    validates :phone, length: {maximum: 15 ,message: 'Exceeds Maximum number of Characters.'} 
    validates :email, email_format: { message: "should be like : [email protected]" } 
    validates :seq_ques_answer, presence: true, length: {maximum: 100,message: 'Exceeds Maximum number of Characters.'} 

    def self.find_first_by_auth_conditions(warden_conditions) 
    conditions = warden_conditions.dup 
    if login = conditions.delete(:login) 
     where(conditions).where(["username = :value OR lower(email) = lower(:value)", { :value => login }]).first 
    else 
     where(conditions).first 
    end 
    end 
end 

입니다 :

<div class="form-group"> 
<%= simple_form_for(@dashboard_user) do |f| %> 
    <% if @dashboard_user.errors.any? %> 
    <ul class="alert alert-danger"> 
    <% for message_error in @dashboard_user.errors.full_messages %> 
     <li> <%= message_error %></li> 
    <% end %> 
    </ul> 
    <% end %> 
    <table class="mytable"> 
    <tr> 
     <td class="col1"> 
      <label for="UserName" >User Name</label> 
     </td> 
     <td class="col2"> 
      <%= f.text_field :username %> 
     </td> 
     <td class="col1"> 
      <label for="Password">Password</label> 
     </td> 
     <td class="col2"> 
      <%= f.text_field :encrypted_password %> 
     </td> 
    </tr> 
    <tr> 
     <td class="col1"> 
      <label for="LastName">Last Name</label> 
     </td> 
     <td class="col2"> 
      <%= f.text_field :last_name %> 
     </td> 
     <td class="col1"> 
      <label for="FirstName">First Name</label> 
     </td> 
     <td class="col2"> 
      <%= f.text_field :first_name %> 
     </td> 
    </tr> 
    <tr> 
     <td class="col1"> 
      <label for="MiddleName">Middle Name</label> 
     </td> 
     <td class="col2"> 
      <%= f.text_field :middle_name %> 
     </td> 
     <td class="col1"> 
      <label for="PhoneNumber">Phone Number</label> 
     </td> 
     <td class="col2"> 
      <%= f.phone_field :phone %> 
     </td> 
    </tr> 
    <tr> 
     <td class="col1"> 
      <label for="EmailID">Email ID</label> 
     </td> 
     <td class="col2"> 
      <%= f.email_field :email %> 
     </td> 
     <td class="col1"> 
      <label for="SecretQuestion">Secret Question</label> 
     </td> 
     <td class="col2"> 
      <%= f.text_field :seq_ques_id %> 
     </td> 
    </tr> 
    <tr> 
     <td class="col1"> 
      <label for="SecretAnswer">Answer</label> 
     </td> 
     <td class="col2"> 
      <%= f.text_field :seq_ques_answer %> 
     </td> 
     <td class="col1"> 
      <label for="Role">User Role</label> 
     </td> 
     <td class="col2"> 
      <select id=:ROLE_ID> 
      <option>Select</option> 
      <option value="1">Admin</option> 
      <option value="2">User</option> 
      </select><br /> 
     </td> 
    </tr> 
    </table> 
    <br> 
    <%= f.button :submit ,class: "btn btn-primary"%> 

<% end %> 
</div> 
</div> 
</div> 
</div> 

지금 내 문제는 점점 새 사용자를 생성 할 때 비밀번호를 비워 둘 수 없습니다.

+1

여러 파일이있는 경우 [gist] (http://gist.github.com)를 대신 사용하는 것이 좋습니다. – Surya

+1

사용자가 필드에 암호화 된 암호를 입력하고 있습니까? – RSB

+0

요점이 더 유용 할 것입니다 – Rubyrider

답변

1

문제가 있습니다. 필드 암호화 된 암호는 사용자 또는 입력 끝에서 암호를 가져 오기위한 것이 아닙니다. Devise가 암호를 암호화하여 저장합니다. 당신의 양식으로.

<%= f.text_field :encrypted_password %> 

지금 다음 두 줄을 추가합니다 :

<%= f.text_field :password %> 
<%= f.text_field :password_confirmation %> 

하는 것은 물론, 컨트롤러는 필요한 PARAMS을 허용되어 있는지 확인

다음 줄을 제거합니다.

def dashboard_user_params 
    params.require(:dashboard_user).permit(:user_id, :username, :normalized_user_name, 
    :password, :password_confirmation, :last_name, :first_name, :middle_name, :phone, :email, :seq_ques_id, 
    :seq_ques_answer, :expire_password_ind, :expire_password_date, :deactivated_ind, 
    :deactivated_date, :role_id, :created_by, :updated_by) 
end 

당신은 암호화 된 암호를 모델에서 검증을 제거합니다.

모든 것이 작동합니다!

1

먼저 비밀번호 필드에 암호화 된 비밀번호를 입력 할 수 없으므로 encrypted_password 필드를 삭제하고 passwordpassword_confirmation 필드를 추가해야합니다.

<%= f.text_field :encrypted_password %> 

이 두 필드

둘째
<%= f.password_field :password %> 
<%= f.password_field :password_confirmation %> 

을 추가, 제거, 당신은 encrypted_password 필드에 대한 검증을 제거해야, 은 모델에서이 줄을 제거

validates :encrypted_password, presence: {message: ' can''t be Blank!'}, length: {maximum: 50 , message: 'Exceeds Maximum number of Characters.'} 

암호 필드에 대한 유효성 검사가 이미 있습니다. y가 Devits의 validatable 모듈에 포함되어 있으므로 모델의 유효성 검사를 추가 할 필요가 없습니다.

희망이 있습니다.

+1

오, 나는 그 검증 항목을 언급하는 것을 잊어 버렸습니다. 그것도 제거되어야합니다. – Rubyrider

관련 문제