Wonky Validation, attr_accessor


#1

I have a somewhat basic question, I have my User Model with the

following attributes:

User.email

User.hashed_password

User.salt

Attr Accessors

User.password_confirmation

User.email_confirmation

class User < ActiveRecord::Base

validates_length_of :password, :within => 5…40
validates_uniqueness_of :email

validates_presence_of :email, :email_confirmation, :password,
:password_confirmation

validates_confirmation_of :password
validates_confirmation_of :email

validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+.)+
[a-z]{2,})$/i, :message => “email address didn’t validate, example
removed_email_address@domain.invalid”

attr_protected :id, :salt

attr_accessor :password, :password_confirmation, :email_confirmation

end

In my users_controller, I have 2 action, change_email and

change_password.

What is the best way to update for just a password change OR email

change (different actions). The reason

I’m asking this is that whenever I try and save just password and

password_confirmation, rails returns

errors for email not being present. (it’s not on the form in

change_password action)

My problem seems to be this, all my users validators are firing even

though I’m only editting a select

attributes from my model.

What’s the best solution guys?

Thanks,

- Viral


#2

Here’s how it should look like:

class User < ActiveRecord::Base

validates_length_of :password, :within => 5…40
validates_uniqueness_of :email

validates_presence_of :email, :password

validates_confirmation_of :password, :if => :password_changed?
#require confirmation only if it has changed
validates_confirmation_of :email, :if => :email_changed?

validates_format_of :email, :with =>
/^([^@\s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})$/i, :message => “email
address didn’t validate, example removed_email_address@domain.invalid”

attr_protected :id, :salt

attr_accessor :password, :password_confirmation, :email_confirmation

end

Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)


#3

Yup, that’s basically what I did when I gutted everything to use
AuthenicatedSystem (acts_as_authenicated)

Thanks ;D

On Feb 25, 11:17 am, Maurício Linhares removed_email_address@domain.invalid