Andrew Perkins
2010-Jul-19 18:33 UTC
How to update user model attributes and bypass a presence_of validation?
Hello, I have a user model which stores a users name, email, and password. I have a separate profile controller which lets the user edit his name, email, and password from the user model. I want the user to be able to update his name separately from his email as well as change his password separately from when he changes his name or email. So on my profile edit page I have 3 forms. One for editing his name, one for his email, then a third form to enter in his current password, what he wants his new password to be, then a confirmation of that password. My problem is that he can''t edit just his name, when I submit the name form the password validation kicks in and won''t let me submit. Can anyone give me some tips on how I could accomplish this please? Thank you. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
pepe
2010-Jul-19 22:09 UTC
Re: How to update user model attributes and bypass a presence_of validation?
Validations usually have an :if/:unless option. That might be helpful. You could, for example, condition the password and e-mail values to validate only if the name is present. On Jul 19, 2:33 pm, Andrew Perkins <andrewp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, I have a user model which stores a users name, email, and password. > > I have a separate profile controller which lets the user edit his name, > email, and password from the user model. > > I want the user to be able to update his name separately from his email as > well as change his password separately from when he changes his name or > email. > > So on my profile edit page I have 3 forms. One for editing his name, one for > his email, then a third form to enter in his current password, what he wants > his new password to be, then a confirmation of that password. > > My problem is that he can''t edit just his name, when I submit the name form > the password validation kicks in and won''t let me submit. > > Can anyone give me some tips on how I could accomplish this please? Thank > you.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Bob
2010-Jul-20 20:22 UTC
Re: How to update user model attributes and bypass a presence_of validation?
There is also the :on option for validations. With combining :on with :if you could essentially set up several validations that are triggered by the specific pages. One validation to ensure presence_of :name, :email and :password :on => :create One validation to ensure presence_of :name :on => :update, :if => :email_and_password_are_not_being_updated One validation to ensure presence_of :email :on => :update, :if => :name_and_password_are_not_being_updated One validation to ensure presence_of :password :on => :update, :if => :email_and_name_are_not_being_updated You would have to write the methods for each :if option to inspect the attribute updates being sent by the form and other validations might be necessary if you have another page to allow edits for all three attributes. Hope this helps. Bob On Jul 19, 2:33 pm, Andrew Perkins <andrewp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, I have a user model which stores a users name, email, and password. > > I have a separate profile controller which lets the user edit his name, > email, and password from the user model. > > I want the user to be able to update his name separately from his email as > well as change his password separately from when he changes his name or > email. > > So on my profile edit page I have 3 forms. One for editing his name, one for > his email, then a third form to enter in his current password, what he wants > his new password to be, then a confirmation of that password. > > My problem is that he can''t edit just his name, when I submit the name form > the password validation kicks in and won''t let me submit. > > Can anyone give me some tips on how I could accomplish this please? Thank > you.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
andrewperk
2010-Jul-21 00:03 UTC
Re: How to update user model attributes and bypass a presence_of validation?
Thank you both Pepe and Bob. I appreciate it. Andrew On Jul 20, 1:22 pm, Bob <rpell...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> There is also the :on option for validations. With combining :on > with :if you could essentially set up several validations that are > triggered by the specific pages. > > One validation to ensure presence_of :name, :email and :password :on > => :create > One validation to ensure presence_of :name :on => :update, :if > => :email_and_password_are_not_being_updated > One validation to ensure presence_of :email :on => :update, :if > => :name_and_password_are_not_being_updated > One validation to ensure presence_of :password :on => :update, :if > => :email_and_name_are_not_being_updated > > You would have to write the methods for each :if option to inspect the > attribute updates being sent by the form and other validations might > be necessary if you have another page to allow edits for all three > attributes. > > Hope this helps. > > Bob > > On Jul 19, 2:33 pm, Andrew Perkins <andrewp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hello, I have a user model which stores a users name, email, and password. > > > I have a separate profile controller which lets the user edit his name, > > email, and password from the user model. > > > I want the user to be able to update his name separately from his email as > > well as change his password separately from when he changes his name or > > email. > > > So on my profile edit page I have 3 forms. One for editing his name, one for > > his email, then a third form to enter in his current password, what he wants > > his new password to be, then a confirmation of that password. > > > My problem is that he can''t edit just his name, when I submit the name form > > the password validation kicks in and won''t let me submit. > > > Can anyone give me some tips on how I could accomplish this please? Thank > > you.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Eric Hao
2010-Jul-21 01:02 UTC
Re: How to update user model attributes and bypass a presence_of validation?
why not just use "update_attribute" ? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.