ruby-on-rails - 使用 has_secure_password 获取密码不能为空

标签 ruby-on-rails

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

这是我的文件:

Gemfile 有并且我做了捆绑安装:

gem '密码'

这是表格

   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> 

这是 Controller 代码:

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

最佳答案

attr_accessor :password, :password_confirmation

在 Teacher 类中注释这一行。

关于ruby-on-rails - 使用 has_secure_password 获取密码不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23723319/

相关文章:

ruby-on-rails - 在条件路由期间未在 Rails 3 中设置 request.subdomain

ruby-on-rails - Rails 3 中的移动风格切换,辅助方法与媒体查询

ruby-on-rails - Ruby 2.0.0,而不是 Rails 站点上推荐的 2.1.0?

javascript - 如何在不创建多维数组的情况下在 ROR 中创建 json 对象

ruby-on-rails - 那里有红隼 gem 吗?

ruby-on-rails - Rails Active_Admin VS。我自己的后端

mysql - 按字段排序

ruby-on-rails - 即使存在标签,RSpec have_tag 也会失败?

ruby-on-rails - 如何允许用户使用 cancan 仅访问他们自己的展示页面?

ruby-on-rails - 我无法让 Trix 编辑器在 Bootstrap 主题中正确显示