I have a problem with my validation of password ! When I set password and password_confirmation to a specific value, no matter what I do it print the error "Password can't be blank". Here is my migration file :
class CreateUsers < ActiveRecord::Migration[5.1]
def change
create_table :users do |t|
t.string :username
t.string :email
t.string :password_digest
t.string :first_name
t.string :last_name
t.integer :role_id, :default => 1
t.timestamps
t.boolean :newsletter_status
end
end
end
My model file :
class User < ActiveRecord::Base
belongs_to :role
has_many :article
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
length: { minimum: 4 },
uniqueness: { case_sensitive: false },
format: { with: VALID_EMAIL_REGEX }
validates :first_name, presence: true,
length: { minimum: 3, maximum: 50 }
validates :last_name, presence: true,
length: { minimum: 3, maximum: 50 }
validates :username, presence: true,
length: { minimum: 3, maximum: 30 },
uniqueness: { case_sensitive: true }
validates :password, length: { minimum: 6, maximum: 100 }, allow_nil: true
has_secure_password
end
My controller file :
class UsersController < ApplicationController
def new
@user = User.new
@user.role_id = 1
end
def create
@user = User.new(user_params)
@user.role_id = 1
if @user.save
flash[:notice] = "L'article à bien été créé"
redirect_to article_path(@article)
else
render 'new'
end
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :username, :email, :password, :password_confirmation, :role_id, :newsletter_status)
end
end
My view form :
<div class="row">
<div class="col-md-6 mb-3">
<%= f.label :password, "Mot de passe" %>
<%= f.password_field :password, class: "form-control", placeholder: "Mot de passe" %>
</div>
<div class="col-md-6 mb-3">
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation, class: "form-control", placeholder: "Mot de passe" %>
</div>
</div>
No matter what I do, it can't create a user. If I do in the rails console user = User.new and enter all parameters it still write password can't be blank in errors.full_messages :
2.3.4 :021 > user = User.create(first_name: 'Name', last_name: 'Surname', email: 'email@example.com', username: 'Username', password: 'password', password_confirmation: 'password', newsletter_status: true)
(0.1ms) begin transaction
Role Load (0.3ms) SELECT "roles".* FROM "roles" WHERE "roles"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
User Exists (0.1ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER(?) LIMIT ? [["email", "email@example.com"], ["LIMIT", 1]]
User Exists (0.1ms) SELECT 1 AS one FROM "users" WHERE "users"."username" = ? LIMIT ? [["username", "Username"], ["LIMIT", 1]]
(0.1ms) rollback transaction
=> #<User id: nil, username: "Username", email: "email@example.com", password_digest: nil, first_name: "Name", last_name: "Surname", role_id: 1, created_at: nil, updated_at: nil, newsletter_status: true>
2.3.4 :022 > user.errors.full_messages
=> ["Password can't be blank"]
I am on rails 5.1.4 and ruby 2.3.3 Thanks !