简体   繁体   English

密码不能为空

[英]Password can't be blank

My dashboard_user controller is: 我的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

My dashboard_use model is 我的dashboard_use模型是

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 : example@example.com" }
  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

My form.html is: 我的form.html是:

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

Now my issue is getting Password can't be blank error when I create new user. 现在我的问题是在创建新用户时出现“密码不能为空”错误。

I got the problem. 我有问题。 The field encrypted password is not for getting the password from the user or input end. 字段加密密码不适用于从用户或输入端获取密码。 Devise will encrypt your password and store it there. Devise将加密您的密码并将其存储在此处。 In your form. 以您的形式。

remove the following line: 删除以下行:

<%= f.text_field :encrypted_password %>

Now add two following lines: 现在添加以下两行:

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

Make sure, your controller is permitting the required params also. 确保您的控制器也允许必需的参数。

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

and you remove validations from your model for encrypted password. 并从模型中删除对加密密码的验证。

Things should work now! 事情现在应该工作了!

First, you can't just enter encrypted password in the password field, so you need to remove encrypted_password field and add password and password_confirmation fields. 首先,您不能只在密码字段中输入加密的密码,因此您需要删除encrypted_password字段并添加passwordpassword_confirmation字段。 Remove 去掉

<%= f.text_field :encrypted_password %>

and add these two fields 添加这两个字段

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

Secondly, you need to remove validation for encrypted_password field, Remove this line from your model 其次,你需要为删除验证encrypted_password场,从模型中删除此行

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

Validation for password fields is already included in Devise's validatable module, so you don't need to add validation for that in your model 密码字段的validatable已包含在Devise的validatable模块中,因此您无需在模型中为此添加验证

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM