Cargo-culting isn’t working…
User:
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
UserController:
def change_password
user = User.find(session[:user_id])
if request.post?
opw = params[:user][:old_password]
npw = params[:user][:password]
if user.verify_password(opw) and opw != npw
user.password = npw
user.save
else
flash.now[:notice] = “Bad Password”
end
end
end
change_password.rhtml:
<% form_for :user do |form| %>
<p>
<label for="user_old_password">Old Password:</label>
<%= form.text_field :old_password, :size => 40 %>
</p>
<p>
<label for="user_password">Password:</label>
<%= form.text_field :password, :size => 40 %>
</p>
<p>
<label for="user_password_confirmation">Confirm:</label>
<%= form.text_field :password_confirmation, :size => 40 %>
</p>
<%= 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.