0

Like other posts, I'm getting an error "Password can't be blank" even though the password is filled in. I've added this wrap_parameters :user, include: [:username, :email, :password, :password_confirmation] to my UsersController but with no luck. Here is my Controller:

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

And here is my Model:

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

Any help would be appreciated ...

brighteyes
  • 141
  • 5
  • Are you sure you're getting `params[:password]`? Double-check spelling and the HTML for your form. – tadman Sep 23 '16 at 01:09
  • Yes, I added a puts user_param in the create function in my Controller and it puts out {"username"=>"john", "email"=>"myemail@gmail.com", "password"=>"MyPassword", "password_confirmation"=>"MyPassword"} so I feel good that the parameters are there. – brighteyes Sep 23 '16 at 02:04
  • It looks like user.save is what's failing/throwing the error, but right before that if statement I print out the user.password and it is set. – brighteyes Sep 23 '16 at 02:36
  • 1
    Changing before_save: to before_validation fixed it. – brighteyes Sep 23 '16 at 02:56
  • Might want to add that as an answer with more description as to how. – tadman Sep 23 '16 at 18:25

0 Answers0