[Rails] Cancel / change the default password validation of devise

Introduction

How to edit user registrations with devise

The password handling in devise is designed so that it cannot be edited (updated) from the viewpoint of security, and even if you try, you cannot edit it and you will be forcibly logged out. Also, even if you edit the view and make it impossible to input, validation will be activated. So you have to turn off the validation when editing (update).

table of contents

  1. Devise installation and various file creation / settings
  2. Devalidation
  3. Validation customization
  4. Edit view page

Development environment

ruby 2.6.5 rails 6.0.0 devise 4.7.3

Implementation

1. Devise installation and various file creation / settings

install devise rails g devise: view model name Routing settings

Now you are ready to go.

2. Devalidation

First, when you install the device, it will cancel the validation that is set by default.

Devalidation on model

model/user.rb


 devise :database_authenticatable, :registerable,
       :recoverable, :rememberable  :validatable  #← Delete this#

Removed: validatable as it controls device validation. You can now change it manually. You can edit (update) in this state, but as it is, everything is off, so the validation at the time of new registration (create) is also off.

3. Validation customization

Since we want to activate validation when newly registering (create), specify the validation timing using the on option.

model/user.rb


 validates :password, on: :create #← create In other words, turn it on only when saving.#
 validates :nickname, presence: true
 validates :name, presence: true

With this, it is possible to turn off when editing (update) and on when newly registering (create).

4. Edit view

By the way, the password change function itself can be done by selecting "Current password" → "New password" ,,,, but passwords and e-mail addresses require strict security. We recommend that you create a dedicated page instead of treating it together with pages such as name and address.

Summary

devise is very convenient when implementing user management functions, and various functions such as user registration are standard equipment. However, various operations are required to customize the specifications to your liking.

Finally

I am a beginner in programming, but I am posting an article in the hope that it will help people who want to become engineers as well as myself. See you next time ~

Recommended Posts

[Rails] Cancel / change the default password validation of devise
[Rails] When the layout change of devise is not reflected
Change the default timezone for the rails app
[Rails] Change the label name of f.label
[Rails 5] How to display the password change screen when using devise
Rails: Japanese localization of validation messages including devise
[Rails] Customize devise validation
[Rails 6] Change redirect destination at the time of new registration / login by devise
[Rails] How to change the column name of the table
[Rails] Introduction of devise Basics
[Rails] Register by attribute of the same model using Devise
When you want to change the MySQL password of docker-compose
[Rails] Where to be careful in the description of validation
Summary of rails validation (for myself)
[Rails] Check the contents of the object
[Rails] Unexpected validation error in devise
[Swift] Change the textColor of UIDatePicker
Explanation of the order of rails routes
[Ruby on Rails] Change the save destination of gem refile * Note
Check the migration status of rails
[Rails] How to change the page title of the browser for each page
[Rails] [Devise] Edit profile without password
Change the save destination of the image to S3 in the Rails app. Part 2
The identity of params [: id] in rails
[Eclipse] Change the color of the vertical ruler
Bootstrap4 Change the color of the hamburger menu
[Swift] Change the color of SCN Node
The process of introducing Vuetify to Rails
[Rails] Implementation of validation that maintains uniqueness
[Rails] devise customization. How to change the redirect page after user registration and editing, and how to skip password input when editing
[Rails] Get the path name of the URL before the transition and change the link destination
[Rails] devise
[Rails] Set validation for the search function using Rakuten API (from the implementation of Rakuten API)
[Order method] Set the order of data in Rails
[Rails] Button to return to the top of the page
[Ruby on Rails] Until the introduction of RSpec
RSpec-Results of reviewing the test code for'users validation'
Change the URL of the application of Eclipse project (Maven).
Change the timezone of the https-portal container to JST
[Rails] Japanese localization of validation error message ~ ja.yml ~
How Microservices Change the Way of Developing Applications
[AndroidStudio] Change the color of the ActionBarDrawerToggle hamburger menu
Change the location folder of Docker image & container
Specify the default value with @Builder of Lombok
[Note] Summary of rails login function using devise ①
[Ruby on Rails] Implementation of validation that works only when the conditions are met
[rails] After option useful when you want to change the order of DB columns
Arguments with default values Take the full_title method of the Rails tutorial as an example
[Rails] Let's dynamically get the threshold of model validation "length check" from table information