[RUBY] I can't edit user information with devise! ~ Update_resource Let's see here ~

An error occurred that the user information could not be updated & could be stopped by validation.

As a premise, the following was added to omit password entry when updating user information.

registrations_controller.rb
def update_resource(resource, params)
    resource.update_without_password(params)
  end

However, it could not be updated due to the following two reasons

devise_parameter_sanitizer The cause was that it was only when: sign_up. Create: account_update as follows

application_controller.rb
def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:email, :nickname])
    devise_parameter_sanitizer.permit(:account_update, keys: [:email, :nickname])
  end

Model validation

The following presence: true warned me "can't be blank!".

users.rb
with_options presence: true do
    VALID_PASSWORD_REGEX = /\A(?=.?[a-zA-Z])(?=.?\d)\w{6,}\z/.freeze
    validates :nickname,length: { maximum: 12 }
    validates :password, format: { with: VALID_PASSWORD_REGEX, message: 'Please set 6 or more single-byte characters including both letters and numbers.' }
  end

Changed the description as follows


with_options presence: true do
    VALID_PASSWORD_REGEX = /\A(?=.?[a-zA-Z])(?=.?\d)\w{6,}\z/.freeze
    validates :nickname,length: { maximum: 12 }

    with_options on: :create do
    validates :password, format: { with: VALID_PASSWORD_REGEX, message: 'Please set 6 or more single-byte characters including both letters and numbers.' }
    end
  end

Supplement on :: create (option to work only during controller create action)

As a result, create (when changing user information) can be left blank!

Recommended Posts

I can't edit user information with devise! ~ Update_resource Let's see here ~
Edit user information with Devise
I can't install lombok with Gradle.
[Rails] How to get the user information currently logged in with devise