Forum: Ruby on Rails Wonky Validation, attr_accessor

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Viral (Guest)
on 2009-02-25 00:29
(Received via mailing list)
# 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
Maurício L. (Guest)
on 2009-02-25 18:18
(Received via mailing list)
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)
Viral (Guest)
on 2009-02-27 05:47
(Received via mailing list)
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>
This topic is locked and can not be replied to.