"密码不能为空白" Rails Rails 4.2.4

时间:2016-09-23 00:59:11

标签: ruby-on-rails ruby

与其他帖子一样,我收到错误"密码不能为空?"即使密码已填写。我已将此wrap_parameters :user, include: [:username, :email, :password, :password_confirmation]添加到我的UsersController但没有运气。这是我的控制器:

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]
  wrap_parameters :user, include: [:username, :email, :password, :password_confirmation]

  # GET /users
  # GET /users.json
  def index
    @users = User.all
  end

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

  # GET /users/new
  def new
    @user = User.new
  end

  # GET /users/1/edit
  def edit
  end

  # POST /users
  # POST /users.json
  def create
    @user = User.new(user_params)
    puts "*********"
    puts user_params

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /users/1
  # PATCH/PUT /users/1.json
  def update
    respond_to do |format|
      if @user.update(user_params)
        format.html { redirect_to @user, notice: 'User was successfully updated.' }
        format.json { render :show, status: :ok, location: @user }
      else
        format.html { render :edit }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.json
  def destroy
    @user.destroy
    respond_to do |format|
      format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def user_params
      params.require(:user).permit(:username, :email, :password, :password_confirmation )
    end
end

这是我的模特:

class User < ActiveRecord::Base
  has_secure_password

  attr_accessor :username, :email, :password, :password_confirmation

  EMAIL_REGEX = /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i
  validates :username, :presence => true, :uniqueness => true, :length => { :in => 3..20 } 
  validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
  validates :password, :confirmation => true #password attr
  validate :confirm_password_match
  validates_length_of :password, :in => 8..20, :on => :create

  before_save :encrypt_password, :prep_data
  after_save :clear_password

  def prep_data
    self.email = email.downcase
    self.username = username.downcase
  end

  def encrypt_password
    if password.present?
      self.password_salt = BCrypt::Engine.generate_salt
      self.password_digest = BCrypt::Engine.hash_secret(password, password_salt)
    end
  end

  def clear_password
    self.password = nil
  end

  def confirm_password_match
    if self.password != self.password_confirmation 
      errors.add(:password, "Passwords must match")
      errors.add(:password_confirmation, "Passwords must match")
    end
  end
end

任何帮助将不胜感激......

0 个答案:

没有答案