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 5937355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:31:48+00:00 2026-05-22T15:31:48+00:00

I am building an authentication, from scratch no Devise or Authlogic, which will save

  • 0

I am building an authentication, from scratch no Devise or Authlogic, which will save three different parts of my :user table independently. I am trying to get it to save the non-essentials, like the users real name and age verification, without a password. While if the the :user wants to change there login details, like a :loginname or :email, then they need to enter their :password in order to effect the change. Same goes for if the :user wants to change their :password.
Here is the model for the :user table.

class User < ActiveRecord::Base
attr_accessor   :password, :old_password, :current_password
attr_accessible :loginname, :email, :password, :password_confirmation,
                 :name, :title_forName, :age_verify, :old_password, 
                 :current_password

email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
loginname_regex = /\A[\w\d]+\z/i
password_regex = /\A(?=.*[\d])(?=.*[A-Z])([1-zA-Z0-1@*#$.]{6,20})\z/

validates :loginname, :presence     => true,
                      :format       => { :with => loginname_regex },
                      :length       => { :maximum => 30},
                      :uniqueness   => { :case_sensitive => false }
validates :email,     :presence     => true,
                      :format       => { :with => email_regex },
                      :uniqueness   => { :case_sensitive => false }
validates :password,  :presence     => true,
                      :on           => :create, 
                      :on           => :change_password_update,
                      :on           => :change_password,
                      # :on           => :update, #This fixes the problem with the password not being validated, but breaks everything else
                      :confirmation => true,
                      :length       => { :within => 6..20 },
                      :format       => { :with => password_regex }
validates :name,      :length       => { :maximum => 75 }
validates :old_password, :presence  => true,
                         :on        => :change_password_update,
                         :on        => :change_password,
                         :format    => { :with => password_regex }
validates :current_password, :presence  => true,
                         :on        => :change_login_update,
                         :on        => :change_login,
                         :format    => { :with => password_regex }

before_save do
     if ( !self.password.blank? || !self.password_confirmation.blank?) #This will keep from a blank password being saved to an encryption.
        :encrypt_password
    end
end

This is update section on the controller for :user

def update
@user = User.find(params[:id])
if params["change_login_update"]
    change_login_update
else
    if params["change_password_update"]
        change_password_update
    else
        if @user.update_attributes(params[:user])
        flash[:success] = "Profile updated."
        redirect_to @user
        else
        @title = "Edit user"
        render 'edit'
        end
    end
end

end

This is for the :user login section

def change_login_update
    if @user.has_password?(params[:user][:current_password])
        if @user.update_attributes(params[:user])
        flash[:success] = "Login details updated."
        redirect_to @user
        else
        @title = "Change Login Details"
        render 'change_login'
        end
    else
        flash[:notice] = "Password Didn't Match Our Records"
        @title = "Change Login Details"
        render 'change_login'
    end
  end

And this is for the :password_change section on the :users contoller

def change_password_update
    if @user.has_password?(params[:user][:old_password])
        if @user.update_attributes(params[:user])
        flash[:success] = "Password updated."
        redirect_to @user
        else
        @title = "Change Password"
        render 'change_password'
        end
    else
        flash[:notice] = "Original Password Didn't Match Our Records"
        @title = "Change Password"
        render 'change_password'
    end   
  end

1 (Updating the non important things) — The submit button on the edit user main page, where it will only change things that don’t effect the :user login, works without incident and everything that needs to change is updated.

2 (Updating the login details) — The submit button is named "change_login_update",:flash works correctly and field validation for :email and :loginname works as well. Upon clicking submit the user is asked for the proper password and it does do password matching before it will save the data, but it will not check to see if the input data for the :current_password is in the correct format. This seems to be an issue with the :on fields that were added to the :user model not functioning.

3 (Updating the password) — The submit button is named "change_password_update",:flash works correctly but the :password and :password_confirmation validations don’t fire. The password match, :old_password, on this works as well. If the fields are all entered correctly the :flash[:success] fires but the password is not updated. If the :on => update, is used the password will save properly but everything else will break because the :password is not available to edit on any other page.

It seems to be a problem with the :on statements not firing correctly for the right sections. This is my first time working with :on paths so any help would be greatly appreciated. Thank you in advance.

  • 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-05-22T15:31:49+00:00Added an answer on May 22, 2026 at 3:31 pm

    My solution was to add :if statements to the validation

    validates :password,  :presence     => true,
                          :on           => :create,
                          :confirmation => true,
                          :length       => { :within => 6..20 },
                          :format       => { :with => password_regex }
    validates :name,      :length       => { :maximum => 75 }
    validates :old_password, :presence  => true,
                              :format    => { :with => password_regex },
                              :if        => :old_password_check?
    validates :current_password, :presence  => true,
                             :if        => :login_check?,
                             :format    => { :with => password_regex }
    validates :password,  :presence     => true,
                           :confirmation => true,
                           :length       => { :within => 6..20 },
                           :format       => { :with => password_regex },
                           :if           => :old_password_check?
    

    after this all the validations worked correctly but the password was not saving correctly and the solution to that was that I had written my before_save incorrectly this is the corrected version

    before_save do
         if( !self.password.blank? )
            encrypt_password
        end
    end
    

    Now everything works perfectly. Thank you all for your help

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im building a web application which is a process management app. Several different employee
I am looking into building an authentication in my ASP.NET application with the following
Building the same project (without any changes) produces binary different exe-files: some small regions
I'm building an application that will require CouchDB's mobile syncing feature. So for each
Im building an application where a few objets outside the mainWindow will need to
Building on How Do You Express Binary Literals in Python , I was thinking
Building a client-side swing application what should be notified on a bus (application-wide message
Building on what has been written in SO question Best Singleton Implementation In Java
When building a VS 2008 solution with 19 projects I sometimes get: The GenerateResource
When building projects in C++, I've found debugging linking errors to be tricky, especially

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.