Validates_confirmation_of not working

Hello to everyone,

Well, very frustrated with this one because I am following the example
straight from the documentation and it is not working.

First when I try:

validates_confirmation_of :password, :on => :password_update
(password_update is a def in my User model which works fine without
this validation in please)

I get the following error:

TypeError in UsersController#update_password
nil is not a symbol

[Full trace at the end of this message.]

However, if I do only this:

validates_confirmation_of :password

It jumps over the validation because no matter what I put in the
password and password_confirmation fields, changes are always saved.

Any advice, greatly appreciated.

Thank you,

GPB


Full trace for validates_confirmation_of :password, :on
=> :password_update

C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
validations.rb:296:in send' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/ validations.rb:296:invalidates_each’
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/
validations.rb:336:in validates_confirmation_of' #{RAILS_ROOT}/app/models/user.rb:8 C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:203:inload_without_new_constant_marking’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:203:in load_file' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:342:innew_constants_in’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:202:in load_file' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:94:inrequire_or_load_without_engine_additions’
#{RAILS_ROOT}/vendor/plugins/engines/lib/engines/rails_extensions/
dependencies.rb:139:in require_or_load' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:248:inload_missing_constant’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:452:in const_missing' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:464:inconst_missing’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:260:in load_missing_constant' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:468:inconst_missing’
#{RAILS_ROOT}/app/controllers/application.rb:9:in admin_authenticate' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ filters.rb:456:insend’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
filters.rb:456:in call' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ filters.rb:435:incall’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
filters.rb:637:in call_filter' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ filters.rb:619:inperform_action_without_benchmark’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
benchmarking.rb:66:in perform_action_without_rescue' C:/ruby/lib/ruby/1.8/benchmark.rb:293:inmeasure’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
benchmarking.rb:66:in perform_action_without_rescue' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ rescue.rb:83:inperform_action’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
base.rb:430:in send' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ base.rb:430:inprocess_without_filters’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
filters.rb:624:in process_without_session_management_support' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/ session_management.rb:114:inprocess’
C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/
base.rb:330:in process' C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/dispatcher.rb:41:indispatch’
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:
113:in handle_dispatch' C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:79:inservice’
C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in service' C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:inrun’
C:/ruby/lib/ruby/1.8/webrick/server.rb:173:in start_thread' C:/ruby/lib/ruby/1.8/webrick/server.rb:162:instart’
C:/ruby/lib/ruby/1.8/webrick/server.rb:162:in start_thread' C:/ruby/lib/ruby/1.8/webrick/server.rb:95:instart’
C:/ruby/lib/ruby/1.8/webrick/server.rb:92:in each' C:/ruby/lib/ruby/1.8/webrick/server.rb:92:instart’
C:/ruby/lib/ruby/1.8/webrick/server.rb:23:in start' C:/ruby/lib/ruby/1.8/webrick/server.rb:82:instart’
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/webrick_server.rb:63:in
dispatch' C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/ webrick.rb:59 C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:ingem_original_require’
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
require' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:495:inrequire’
C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/
dependencies.rb:342:in new_constants_in' C:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/ dependencies.rb:495:inrequire’
C:/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/server.rb:39
C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
gem_original_require' C:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:inrequire’
script/server:3

On 24 Oct 2007, at 10:55, GPB wrote:

(password_update is a def in my User model which works fine without
this validation in please)

You can’t do that. :on must be one of :save, :create or :update

Fred

Hello again…

I will answer my first question.

I thought the :on option could take any def from the controller and
apply the validation only when that action was called. But now I see
it only takes :save, :create and :update.

However, even when putting :update as the :on option, it is still
bypassing the validation even though the data in the fields are not
equal.

And one correction in my previous post, password_update is a def in my
controller, not in my model, like I stated previously.

Thanks again,

On 24 Oct 2007, at 11:04, GPB wrote:

Hello again…

I will answer my first question.

I thought the :on option could take any def from the controller and
apply the validation only when that action was called. But now I see
it only takes :save, :create and :update.

It’s not a controller thing (how would the model know the action that
was causing it to be saved), it’s a model thing: is the instance
being saved for the first time or all the time

However, even when putting :update as the :on option, it is still
bypassing the validation even though the data in the fields are not
equal.

Can’t guess without seeing at least some code.

Fred

Hi Fred,

I went ahead and coded the validation myself.

However, I think the problem has to do with this, used for the
encryption of the password.

def password=(value)
if !value.blank?
write_attribute(“password”, Digest::SHA1.hexdigest(value))
end
end

Because I got it to the point that the validation worked, but it
always returned that the values didn’t match.

When I took out the lines for encryption it worked perfectly. So it
seems it looks to compare the password string encrypted with the
password_confirmation string not encrypted.

Care to explain how to work around this?

Thank you,

Hi DHH, you are right, it was a type on my part when writing the post,
anyway Fred already explained I can’t do that for the :on condition.

Either way, thank you.

On Oct 24, 12:17 pm, Dhh R. [email protected]

validates_confirmation_of :password, :on => :password_update
(password_update is a def in my User model which works fine without
this validation in please)

I get the following error:

TypeError in UsersController#update_password

Well i am a bit confused why u have called password_update and you are
getting update_password error…
check your model once again
DHH

On 24 Oct 2007, at 12:14, GPB wrote:

if !value.blank?

I think if you do the hashing of the password in a before_update
you’ll be ok.

Fred

Thank you Fred, that worked.

On Oct 24, 1:18 pm, Frederick C. [email protected]