Validation error: "Password can't be blank" but it isn't blank

It keeps saying that the password field and the password_confirmation field is blank, even when I use the rails console to create a new user. Also, the error message for the password confirmation repeats and I'm not sure why. Any help would be greatly appreciated.

Screenshot of error messages

User Model

class User < ApplicationRecord
  has_secure_password
  validates_confirmation_of :password
  attr_accessor :password
  validates :password, length: 6..100, confirmation: true
  validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, on: :create }
  before_save :encrypt_password
  validates_presence_of :password_confirmation, if: lambda {| u| u.password.present? }


  def encrypt_password
    self.password_salt = BCrypt::Engine.generate_salt
    self.password_hash = BCrypt::Engine.hash_secret(password,password_salt)

  end
end

User controller

  class UsersController < ApplicationController
  def new
    @user = User.new
  end

  def index
  end

  def show
    @user = User.find(params[:id])
  end

  def create
    @user = User.new(user_params)
    if @user.save and user.valid?
      session[:user_id] = @user.id
      render 'users/new'
    else
      render :action => "new"
    end
  end

  private
  def user_params
    params.required(:user).permit(:name, :email, :password, :password_confirmation)
  end
end

User/Sign-up view

<%= stylesheet_link_tag "signup.css" %>

<div id = signup_image></div>
<div id = signup-background>
  <div id = signup-text>
    <h1 style = "color: rgb(40,50,60);">Sign up</h1>
    <%= form_for :user,url: '/users' do |f| %>
      <br> <%= f.text_field :name, placeholder: "Name", class: 'signup-textfield'%><br>
      <br><%= f.text_field :email, placeholder:"email@gmail.com", class: 'signup-textfield' %><br>
      <br><%= f.password_field :password, placeholder: "Password",class: 'signup-textfield' %><br>
      <br><%= f.password_field :password_confirmation, placeholder: "Confirm password",class: 'signup-textfield' %><br>
      <br>
      <%= f.submit id: "signup-button"%>
    <% end %>
    <br>
    <small>Already have an account?  <%=link_to "Login here", "/login" %></small>
  </div>
</div>

User new view

<div id = signup_image></div>
<div id = signup-background>
  <div id = signup-text>
    <h1 style = "color: rgb(40,50,60);">Sign up</h1>
      <div class="row">
        <div class="span6 offset3">
            <%= render :partial => 'shared/error_message_form' %>
        </div>
      </div>
    <%= form_for @user do |f| %>
        <br> <%= f.text_field :name , placeholder: "Name", class: 'signup-textfield'%>
        <br><%= f.text_field :email,  placeholder:"email@gmail.com", class: 'signup-textfield' %>
        <br><%= f.password_field :password_digest, placeholder: "Password",class: 'signup-textfield' %>
        <br><%= f.password_field :password_confirmation, placeholder: "Confirm password",class: 'signup-textfield' %>
        <br>
        <%= f.submit id: "signup-button"%>
    <% end %>
    <br><br>

    <small>Already have an account?  <%=link_to "Login here", "/login" %></small>
  </div>
</div>

Partial for Error Message

<% if @user.errors.any? %>
  <div id="error_explanation">
    <div class="alert alert-error">
      <%= pluralize(@user.errors.count, "error")%> detected while signing up.
    </div>
    <ul>
      <br>
      <% @user.errors.full_messages.each do |msg| %>
        <li>* <%= msg %></li>
      <% end %>
    </ul>
  </div>
<% end %>

en.yml file

en:
  hello: "Hello world"
  activerecord:
    models:
      user:
    attributes:
      user:
        email: "Email address"
    errors:
      models:
        user:
          attributes:
            email:
              blank: "cannot be empty"
              taken: "%{value} is already taken"


from Recent Questions - Stack Overflow https://ift.tt/3h42uLk
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations