[RUBY] [rails] About devise defaults

About the introduction and default of devise


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'


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

#Install Gem
% bundle install


#Start the server
% rails s


#Create devise config file
% rails g devise:install


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

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


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

Next is the creation of the table


#Perform migration
% rails db:migrate

OK if the table is created in the database!

Finally, don't forget the following!


# 「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>


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


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.)


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])

Pay attention to the 14th line!


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!


How to remove the default validation



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

Just do this!


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


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