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!
?