[Beginner] How to use devise Change settings from introduction


I used devise for studying and created a login area. By default, the user name and password are used for authentication, but this time we will change the settings to authenticate with the employee number and password.


Ruby 2.5.3 Ruby on Rails 5.2.4 Devise 4.7.1


スクリーンショット 2020-06-24 16.00.39.png スクリーンショット 2020-06-24 16.00.56.png I've adjusted the appearance with bootstrap, but I will change the settings so that it looks like this. # Implementation


gem 'devise'
$ bundle install

● devise settings

$ rails generate devise:install

I get a message like this. Since I am a beginner, I was surprised at the error message, but if this appears, it is a success.

create  config/initializers/devise.rb
      create  config/locales/devise.en.yml
Some setup you must do manually if you haven't yet:
  1. Ensure you have defined default url options in your environments files. Here
 	is an example of default_url_options appropriate for a development environment
 	in config/environments/development.rb:
   	config.action_mailer.default_url_options = { :host => 'localhost:3000' }
 	In production, :host should be set to the actual host of your application.
  2. Ensure you have defined root_url to *something* in your config/routes.rb.
 	For example:
   	root :to => "home#index"
  3. Ensure you have flash messages in app/views/layouts/application.html.erb.
 	For example:
   	<p class="notice"><%= notice %></p>
   	<p class="alert"><%= alert %></p>
  4. You can copy Devise views (for customization) to your app by running:
   	rails g devise:views

● Message content

  1. When you send an authentication email such as new registration, set the approval link URL in the text of the email.


config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
  1. Route setting It is set to jump to the route after registering as a member.

  2. Embed a flash message. It is used when you want to display the flash when logging in or out. I embedded it in a common view.


 <p class="notice"><%= notice %></p>
 <p class="alert"><%= alert %></p>
  1. To customize the view
$ rails g devise:views

● Change devise settings


Around the 43rd line
Specify the employee number for the authentication key
- config.authentication_keys = [:email]
+ config.authentication_keys = [:employee_number]
Around the 55th line
Authentication key values are not case sensitive
- config.case_insensitive_keys = [:email]
+ config.case_insensitive_keys = [:employee_number]
Around the 60th line
Get rid of spacebar
- config.strip_whitespace_keys = [:email]
+ config.strip_whitespace_keys = [:employee_number]

● Create a user model

$ rails g devise user


#The following three methods use the user name as the authentication key, so
#Overriding and disabling unnecessary methods.

def email_required?

  def email_changed?
  def will_save_change_to_email?


class DeviseCreateUsers < ActiveRecord::Migration[5.2]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :employee_number,    null: false, default: ""
      t.string :encrypted_password, null: false, default: ""
## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      # t.integer  :sign_in_count, default: 0, null: false
      # t.datetime :current_sign_in_at
      # t.datetime :last_sign_in_at
      # t.string   :current_sign_in_ip
      # t.string   :last_sign_in_ip

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at
    t.timestamps null: false

Change here!!
     - add_index :users, :email,      unique: true
     + add_index :users, :employee_number,      unique: true
      add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
$ rails db:migrate

● Change controller


class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :configure_permitted_parameters, if: :devise_controller?

  #Override method.
  def configure_permitted_parameters
    sign_up_params = [:employee_number, :password, :password_confirmation]
    sign_in_params = [:employee_number, :password, :remember_me] 

      # account_update, sign_in, sign_up,Redefine the field of
    devise_parameter_sanitizer.permit(:sign_up, keys: sign_up_params)
    devise_parameter_sanitizer.permit(:sign_in, keys: sign_in_params)
    devise_parameter_sanitizer.permit(:account_update, keys: account_update)

This completes the settings. All you have to do is change the view and you're done.


[* Rails *] How to use devise (rails5 version) Ruby on Rails beginners tried using gem Devise.

Recommended Posts

[Beginner] How to use devise Change settings from introduction
How to use JUnit (beginner)
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
[Introduction to Rails] How to use render
How to change from HTML to Haml
[Note] How to use Rails 6 Devise + cancancan
How to use Map
How to use rbenv
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
From the introduction of devise to the creation of the users table
[rails] How to use devise helper method before_action: authenticate_user!
[Beginner] Discover the N + 1 problem! How to use Bullet
How to change from Oracle Java 8 to Adopt Open JDK 9
How to use Chain API
How to use java Optional
How to use Ruby return
How to use @Builder (Lombok)
How to use java class
How to use Big Decimal
[Java] How to use removeAll ()
How to use rails join
How to use Java Map
How to use dependent :: destroy
How to use Eclipse Debug_Shell
[Rails] How to use validation
How to use Java variables
[Rails] How to use authenticate_user!
How to use GC Viewer
Change from SQLite3 to PostgreSQL
How to use Lombok now
[Creating] How to use JUnit
[Rails] How to use Scope
How to change application.properties settings at boot time in Spring boot
[Rails 5] How to use gem gon ~ How to pass variables from Rails to JS ~
How to use the link_to method
How to use Lombok in Spring
How to use StringBurrer and Arrays.toString.
How to use arrays (personal memorandum)
How to use scope (JSP & Servlet)
How to use the include? method
How to use the form_with method
[Rails] How to use flash messages
How to use EventBus3 and ThreadMode