密码不能为空

时间:2014-11-05 06:36:13

标签: ruby-on-rails ruby authentication devise

我的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模型是

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

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

现在我的问题是在创建新用户时,密码不能出现空白错误。

2 个答案:

答案 0 :(得分:1)

我遇到了问题。字段加密密码不是用于从用户或输入端获取密码。 Devise将加密您的密码并将其存储在那里。在你的表格中。

删除以下行:

<%= f.text_field :encrypted_password %>

现在添加以下两行:

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

确保您的控制器也允许所需的参数。

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 :(得分: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.'}

验证密码字段已包含在Devise的validatable模块中,因此您无需在模型中添加验证

希望这有帮助!