Corey Quillen
Corey Quillen

Reputation: 1576

Rails password can't be blank error

I am trying to create a user signup form in Rails 4.1.6. I keep getting a 'password can't be blank' error. I can see that both the password and password_confirmation are in the params hash but not in the params[:user] sub-hash. I cannot figure out why for the life of me. Any help will be greatly appreciated.

User Model:

class User < ActiveRecord::Base

    belongs_to :company

    has_secure_password

end

Users Controller:

class UsersController < ApplicationController

    respond_to :json

    def index
        @users = User.all
        respond_with(@users)
    end

    def create
        @user = User.new(user_params)
        @user.save
        respond_with(@user)
    end


    private

        def user_params
            params.require(:user).permit(:given_name, :family_name, :email, :password, :password_confirmation)
        end

end

Upvotes: 0

Views: 1307

Answers (1)

ihaztehcodez
ihaztehcodez

Reputation: 2133

It sounds like your parameters are not being sent to the server correctly.

password should be sent as user[password] and password_confirmation should be sent as user[password_confirmation].

See documentation for hash and array parameters.

Alternatively, adding wrap_parameters to the controller will wrap parameters into a nested hash.

wrap_parameters :user, include: [:given_name, :family_name, :email, :password, :password_confirmation]

See documentation for ActionController::ParamsWrapper.

Upvotes: 3

Related Questions