[rails] About devise defaults

About the introduction and default of devise

Why

When I looked up the article about devise, there was only an article to make a controller for devise, and I wanted to complete it with only application.controller </ font>. Also, at that time, I did not know the range that devise has by default, and it took time to resolve the error because I wrote extra columns and validation, so please refer to this.

Introducing devise

Gemfile(Last line)


gem 'devise'

Terminal


#The current directory is~/projects/Confirm that it is pictweet
% pwd

#Install Gem
% bundle install

Terminal


#Start the server
% rails s

Terminal


#Create devise config file
% rails g devise:install

Terminal


#Create User model with devise command
% rails g devise user

If devise_for: users </ font> is included as shown below, it's OK!

routes.rb


Rails.application.routes.draw do
  devise_for :users
  root to: 'tweets#index'
  resources :tweets
end

Next is the creation of the table

Terminal


#Perform migration
% rails db:migrate

OK if the table is created in the database!

Finally, don't forget the following!

Terminal


# 「ctrl +Quit the local server with "C"

#Start the local server again
% rails s

Now that devise has been introduced, all you have to do is implement it as you like.

This time about the default !!! </ font>

c5f52229a3b1938b4297854ebbd2d440.png

This is not so relevant, but only name, email, and password are applied in validates, and when I check the database, the password part is registered as a column with encrypted_password </ font>. I'd like to rush "validates doesn't have encrypted_password?", But encrypted_password is encrypt the password and save it in the table </ font>, so validates is ok just by applying password. By the way, password_confirmation </ font> is ok without applying validates.

The story has derailed, but here

a23f24d4b5f8cf20bd382f240b123723.png

Pay attention to the 13th line.

By installing devise, you can use devise_parameter_sanitizer method </ font>. It can be used when registering as a user with devise, and it is a method that "allows a specific column". Define a devise_parameter_sanitizer method </ font> for this method.

devise_parameter_sanitizer method

By combining the permit method, you can specify and include the newly added column for the strong parameter defined in devise. So, if it is a normal new registration and login function, it can be completed in application.contoroller without having to create a devise controller. (It is better to make a controller properly when transitioning over multiple pages due to new registration etc.)

java:application.controller.rb


private
def configure_permitted_parameters  #Method names are customary
  #Allow parameters for devise User model
  devise_parameter_sanitizer.permit(:devise process name, keys: [:Keys to allow])
end

Pay attention to the 14th line!

java:application.controller.rb


devise_parameter_sanitizer.permit(:sign_up, keys: [:name])

In addition to: name, you must put : email </ font> or : password </ font> in [] after keys :. not. However, devise's default is to handle it behind the scenes, so just : name </ font> is ok!

Digression

How to remove the default validation

this

java:application.controller.rb


devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

Just do this!

java:application.controller.rb


devise :database_authenticatable, :registerable,
         :recoverable, :rememberable

Summary

devise is a wonderful gem that is easy to install and can easily implement new registration, login, and logout functions, but behind the scenes it is too convenient because various defaults are reflected without permission and extra errors are likely to occur. At the very least, I have introduced a simple default.

Recommended Posts