There is a user model:

 class User < ActiveRecord::Base ... validates :name, presence: true, length: { maximum: 50 } VALID_EMAIL_REGEX = /\A[\w+\-.]+@[az\d\-]+(\.[az]+)*\.[az]+\z/i validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false } validates :password, length: { minimum: 8 } has_secure_password ... 

It is necessary to make the method of changing email'a.

Made a method in the controller:

 class UsersController < ApplicationController ... def update_email @user = User.find(params[:id]) if @user.update_attributes(user_email_params) flash[:success] = "Адрес электронной почты изменен" redirect_to @user else render 'settings' end end ... private def user_email_params params.require(:user).permit(:email) end 

In this embodiment, it does not work, because when you call update_email , it displays an error due to the invalid password. If you use user.update_attribute(...) or user.save! , the validity of the attribute being changed is not checked.

How to make it so that the validity of the password is not checked, and only validity of the email checked?

Is there any solution using rails or just write your own validation method and use it with .save! ?

    3 answers 3

    You can use the conditions for if/unless

     class User < ActiveRecord::Base ... attr_accessor_with_default :ignore_password_validation, false validates :password, length: { minimum: 8 }, unless: :ignore_password_validation 

    Then the controller method will be as follows:

     class UsersController < ApplicationController ... def update_email @user = User.find(params[:id]) if @user.update_attributes(user_email_params.merge(ignore_password_validation: true)) flash[:success] = "Адрес электронной почты изменен" redirect_to @user else render 'settings' end end 

      In the model, you can create a group of validations. For example:

       validation_group :email do validates_presence_of :email end 

      Then, in the controller write the following:

       def update_email @user.email = params[:email] @user.update_attribute(:email, params[:email]) if @user.group_valid?(:email) end 

      Method group_valid?(:email) - performs the group of validations described in the model :email Method update_attribute - saves the field bypassing validation

      • I installed the gem github.com/adzap/grouped_validations , made validation_group. But it does not work and gives the error NoMethodError: undefined method `_run_validate_name_callbacks' for # <User: 0x000000036e2398> - Ildar Sharifullin
      • @ IldarSharifullin Update the question by posting the contents of the model, and the controller (or, an action that saves Email) - MAXOPKA

      If this is a devise, then everything is in the wiki. Update without password

      You can also specify a specific action for validation using the on option :

      • Please describe your decision in more detail. You can include sample code, explain the solution, point out specific errors, provide links with more detailed analysis of the problem, etc. A detailed and high-quality answer will help a lot more people who are faced with a similar problem. - Nick Volynkin
      • This is not a devise. Helper has_secure_password from ActiveRecord. And to on it would be possible to give a small example, and not to send by reference, which may die one day. - D-side