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.
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.
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.