Neha Narlawar
Neha Narlawar

Reputation: 229

Password can't be blank in Rails (Using has_secure_password)

I am totally new to Rails and I know many questions already exists for this issue on StackOverflow but I tried almost all the solutions but none of the solution is working for me.

I am trying to implement authentication in my rails project using has_secure_password and I followed all the steps mentioned in rails documentation. I am getting "Password can't be blank " error message after submitting create user form even when i am inputting password and confirm password values in input box.

Please suggest if I am missing anything.

Steps which I followed are- 1) Added below line in gem file - gem 'bcrypt', require: 'bcrypt' 2) bundle install 3) My model code-

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

4) My View code -

<div>
          <%= f.label :name %>:
          <%= f.text_field :name, size: 40 %>
        </div>
        <div>
          <%= f.label :password, 'Password' %>:
          <%= f.password_field :password, size: 40 %>
        </div>
        <div>
          <%= f.label :password_confirmation, 'Confirm' %>:
          <%= f.password_field :password_confirmation, size: 40 %>
        </div>
        <div>

5) My Controller Code -

def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to users_url,notice: "User #{@user.name} 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

Upvotes: 3

Views: 5393

Answers (3)

Neha Narlawar
Neha Narlawar

Reputation: 229

Thanks a lot everyone for helping me.I am able to solve this issue.

I added below line of code in my Controller

 wrap_parameters :user, include: [:name, :password, :password_confirmation]

Documentation- http://api.rubyonrails.org/classes/ActionController/ParamsWrapper.html

and this solved my problem.

Upvotes: 6

Jason Adrian Bargas
Jason Adrian Bargas

Reputation: 254

Try to follow the steps here: See Listing 7.17

Sorry, haven't checked your controller and your comment :) This should do the trick

Upvotes: 0

roob
roob

Reputation: 1136

You should add the password-related fields to user_params.

def user_params
  params.require( :user ).permit( :password, :password_confirmation, :etc1, :etc2 )
end

Upvotes: 0

Related Questions