Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7787907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:49:18+00:00 2026-06-01T20:49:18+00:00

I have table with fields: email , password_hash and salt . On user registration,

  • 0

I have table with fields: email, password_hash and salt.

On user registration, I want to validate password and password confirmation, but I always get error that says can't be blank for password input.

This is my model:

attr_accessor :password

validates :password, :presence     => true,
                     :confirmation => true,
                     :length       => { :within => 6..40 }

I guest I can’t validate field that is not it the database, but how to make validation on password that user enters?

I don’t have password field in database because I encypt that password input I get and then I store it in db password_hash field.

EDIT :

This is database table:

class CreateStudents < ActiveRecord::Migration
  def change
    create_table :students do |t|
      t.string :first_name
      t.string :last_name
      t.string :email
      t.string :password_hash
      t.string :salt

      t.boolean :activated

      t.timestamps
    end
  end
end

This is the view for registration:

<%= form_for @student, url: {action: 'create'} do |form| %>

  <p style="font-size:smaller; color:red">
  <% @student.errors.messages.each do |att, msg| %>
    <%= msg[0] %><br>
  <% end %>
  <p>
  <%= form.label :first_name %><br>
  <%= form.text_field :first_name %>
  <p>
  <%= form.label :last_name %><br>
  <%= form.text_field :last_name %>
  <p>
  <%= form.label :email %><br>
  <%= form.text_field :email %>
  <p>
  <%= form.label :password %><br>
  <%= form.password_field :password %>
  <p>
  <%= form.label :password_confirmation %><br>
  <%= form.password_field :password_confirmation %>
  <p>
  <%= form.submit %>

<% end %>

This is the controller:

def sing_up
    @student = Student.new
    render 'sing_up'
end

def create
    @student = Student.new
    @student.first_name = params[:student][:first_name]
    @student.last_name = params[:student][:last_name]
    @student.email = params[:student][:email]

    @student.salt = BCrypt::Engine.generate_salt
    @student.password_hash = BCrypt::Engine.hash_secret(params[:student][:password], @student.salt)
    if @student.save
        redirect_to controller: 'singups', action: 'index'
    else
        render 'sing_up'
    end
end

and finnaly, this is model

class Student < ActiveRecord::Base
  attr_accessor :password
    validates :first_name, :length => { minimum: 3, message: "first_name" }
    validates :last_name, :length => { minimum: 3, message: "last_name" }
    validates :email, :presence => { message: 'email' },
              :format => { with: /\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+\z/ , message: 'email format' },
              :uniqueness => { message: 'uniqueness?' }
    validates :password, :confirmation => { message: 'password' },
                         :length => { minimum: 6, message: 'password length' }
end

Every time user enters password it fails on first validation :confirmation => { message: 'password' }, no matter what the password was.

If I remove validates :password part, everything works fine, but password that user enters is not validated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:49:19+00:00Added an answer on June 1, 2026 at 8:49 pm

    You’re validating the user’s password attribute but you never assign it a value in the first place. At the least, you need to add this to your controller code:

    @student.password = params[:student][:password]
    @student.password_confirmation = params[:student][:password_confirmation]
    

    However, the more concise way is to use mass-assignment – get rid of all the @student.xxx = yyy and replace it with this:

    @student = Studen.new(params[:student])
    

    Then, move your password hashing method into the model and trigger it automatically before each save if the password attribute is present:

    class User < ActiveRecord::Base
      # ...
      before_save :hash_password, :if => proc{ |u| !u.password.blank? }
      # ....
    
      protected
    
      def hash_password
        self.salt = BCrypt::Engine.generate_salt
        self.password_hash = BCrypt::Engine.hash_secret(password, salt)
      end
    end
    

    This way you don’t have to do anything in the controller but this:

    def create
      @student = Student.new(params[:student])
      if @student.save
        redirect_to controller: 'singups', action: 'index'
      else
        render 'sing_up'
      end
    end
    

    and have all other logic in your model, where it belongs.

    EDIT: In order for mass assignment to work with the latest versions of Rails 3, you’ll need to make the attributes you want to assign like that attr_accessible, like this:

    class Student < ActiveRecord::Base
      # ...
      attr_accessible :first_name, :last_name, :email, :password # but NOT the hash or salt!!
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using rails 3 with devise. I have a User table with fields: email,
I have a database table for users which contains common fields like password, email
I have a table with three fields, FirstName, LastName and Email. Here's some dummy
I have a table with three fields, User , City and Country , where
I have a form with three fields Name, Email Address & Password. now i
When designing user table what would be the must have fields from the security/user
I have my Table structure like this :: ATT_Table : Fields - Act_ID, Assigned_To_ID,
I have table with some fields that the value will be 1 0. This
I have a table the fields are Id1 , ID2 , Points1 , Points2
I have Access table with fields, in which there is some data and path

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.