Changing password not validating_confirmation_of

Cargo-culting isn’t working…

attr_reader :password # Virtual attribute

validates_presence_of :username
validates_uniqueness_of :username

attr_accessor :password_confirmation
validates_confirmation_of :password

& etc from AWDR with use of a hashed password

def change_password
user = User.find(session[:user_id])
opw = params[:user][:old_password]
npw = params[:user][:password]
if user.verify_password(opw) and opw != npw
user.password = npw
else[:notice] = “Bad Password”


<%= error_messages_for 'user' %> Changing Password
<% form_for :user do |form| %>
    <label for="user_old_password">Old Password:</label>
    <%= form.text_field :old_password, :size => 40 %>

    <label for="user_password">Password:</label>
    <%= form.text_field :password, :size => 40 %>

    <label for="user_password_confirmation">Confirm:</label>
    <%= form.text_field :password_confirmation, :size => 40 %>

  <%= submit_tag "Change Password", :class => "submit" %>
<% end %>

I’m reasonably certain that validates_confirmation_of is not being
invoked, but I have no idea why. Assigning user.password_confirmation
does not help. The parameters are coming in just fine.

I’m not sure if this will fix your particular issue, but you may want
to remove your own attr_accessor for the password_confirmation
attribute, since validates_confirmation_of provides its own

The book has it & discussions here seem to confirm that it is
required. I’ll check, though.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs