使用has_secure_password获取密码不能为空

时间:2014-05-18 15:02:03

标签: ruby-on-rails

在Rails 4.x中使用has_secure_password功能时,获取密码不能为空:

以下是我的文件:

Gemfile有&我做了捆绑安装:

gem' bcrypt'

这是表格

   create_table :teachers do |t|

   t.string :firstname, null: false
   t.string :lastname, null: false
   t.string :email, null: false
   t.string :cellphone
   t.string :username, null: false
   t.string :password_digest, null: false
   t.string :addr_streetno
   t.integer :addr_aptno
   t.string :addr_city
   t.string :addr_state
   t.integer :addr_zip
   t.binary :photo, :limit => 0.5.megabyte

   t.timestamps
end 

这是模型文件:

 class Teacher < ActiveRecord::Base

    has_many :students

    has_secure_password
    attr_accessor :password, :password_confirmation
    validates_presence_of :password, :on => :create

    EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
    validates :username, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
    validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
end

这是以下格式的密码字段代码:

<tr>
   <th>Password</th>
   <td><%= f.password_field :password %></td>
 </tr>
 <tr>
   <th>Confirm Password</th>
   <td><%= f.password_field :password_confirmation %></td>
 </tr>
 <tr> 

这是控制器代码:

class TeacherController < ApplicationController
  def index
      @teachers= Teacher.all 
  end

  def new
      @teacher = Teacher.new
  end

  def create
      @teacher = Teacher.new(teacher_params)
      if @teacher.save
        redirect_to :action => 'index'
      else
        render :action => 'new'
      end
  end

  def edit
  end

  def show
  end

private

    def teacher_params
      params.require(:teacher).permit(:firstname, 
                                      :lastname, 
                                      :dob, 
                                      :email, 
                                      :cellphone, 
                                      :username, 
                                      :password,
                                      :password_confirmation,
                                      :addr_streetno, 
                                      :addr_aptno, 
                                      :addr_city, 
                                      :addr_state,
                                      :addr_zip, 
                                      :photo)
    end
end

4 个答案:

答案 0 :(得分:2)

attr_accessor :password, :password_confirmation

在教师班上评论此行。

答案 1 :(得分:1)

在模型中,您有这一行,它定义了您的密码长度。将其更改为您的要求:

validates_length_of :password, :in => 6..20, :on => :create

答案 2 :(得分:1)

允许在您的控制器中encrypted_password

params.require(:teacher).permit(:encrypted_password)

但是不应该允许,因为用户不应该更改encrypted_password的值。从列表中删除encrypted_password,然后添加passwordpassword_confirmation

params.require(:teacher).permit(:password, :password_confirmation)

答案 3 :(得分:1)

我得到的是#34;密码不能为空白&#34;验证错误消息,使用Rails 4和has_secure_password

我的模特看起来像这样:

class User < ActiveRecord::Base
  validates :email, presence: true, uniqueness: true
  has_secure_password
end

奇怪的是,将validates行移到has_secure_password行以下会修复此问题。这很有效:

class User < ActiveRecord::Base
  has_secure_password
  validates :email, presence: true, uniqueness: true
end

我想我会分享,尽管这个问题很老,因为它是搜索此问题的最佳结果,而其他所有问题都没有提及此