[Rails] How to install devise


This is Qiita's first post. It is written by a beginner, so there may be mistakes. If you have any suggestions or advice, I would appreciate it if you could teach me. Thank you.


What is devise

devise is a gem that makes it easy to implement an authentication function in a web application. The authentication function is simply a login and log-out function. Since user information is handled, it is difficult to create from scratch due to security concerns and man-hours. By using devise, the above problems can be solved and the implementation of the authentication function becomes easy.

Introducing devise

1. Install gem

Describe the following in the Gemfile.


gem 'devise'

After saving, enter the following command to load it into the application.


$ bundle install

This completes the installation.

2. devise initial settings

Next, enter the following command to make the initial settings for devise.


$ rails g devise:install

If the following screen is displayed, it is successful.


      create  config/initializers/devise.rb
      create  config/locales/devise.en.yml

Depending on your application's configuration some manual setup may be required:

  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', port: 3000 }

     In production, :host should be set to the actual host of your application.

     * Required for all applications. *

  2. Ensure you have defined root_url to *something* in your config/routes.rb.
     For example:

       root to: "home#index"
     * Not required for API-only Applications *

  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>

     * Not required for API-only Applications *

  4. You can copy Devise views (for customization) to your app by running:

       rails g devise:views
     * Not required *


By the way, when I started using devise, I sometimes skipped this initial setting and got an error ... If that doesn't work, you may want to check if the initial settings have been made.

3. Display the login screen

Create a User model using the devise function. If you want to use a different model name, use the User part as that model name. Note that it will be ** rails g devise model name **, unlike when creating a normal model.


$ rails g devise User

If the following screen is displayed, it is successful.


    invoke  active_record
    create    db/migrate/20201103032107_devise_create_users.rb
    create    app/models/user.rb
    invoke    test_unit
    create      test/models/user_test.rb
    create      test/fixtures/users.yml
    insert    app/models/user.rb
      route  devise_for :users

Load the created migration file. A migration file is simply a blueprint for a table. The second line of the above screen, db/migrate/created date, month, month, day, hour, minute, second, _devise_create_users.rb is the migration file.


$ rails db:migrate

If the following screen is displayed, it is successful.


== 20201103032107 DeviseCreateUsers: migrating ================================
-- create_table(:users)
   -> 0.0046s
-- add_index(:users, :email, {:unique=>true})
   -> 0.0014s
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.0014s
== 20201103032107 DeviseCreateUsers: migrated (0.0076s) =======================

At this point, devise installation is complete. You can access the login screen at the following URL. (You can check the URL of each screen with the rails routes command)

/users/sign_in login.png


Check devise settings

When setup is complete, routes will be automatically added to config/routes.rb and users will be included in the URL when using devise.


devise_for :users

In addition, the following description will be added to app/models/user.rb. The basic functionality of devise is described by default.


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

Login screen customization

Run the following command to create a view.


$ rails g devise:views

If the following screen is displayed, it is successful.


    invoke  Devise::Generators::SharedViewsGenerator
    create    app/views/devise/shared
    create    app/views/devise/shared/_error_messages.html.erb
    create    app/views/devise/shared/_links.html.erb
    invoke  form_for
    create    app/views/devise/confirmations
    create    app/views/devise/confirmations/new.html.erb
    create    app/views/devise/passwords
    create    app/views/devise/passwords/edit.html.erb
    create    app/views/devise/passwords/new.html.erb
    create    app/views/devise/registrations
    create    app/views/devise/registrations/edit.html.erb
    create    app/views/devise/registrations/new.html.erb
    create    app/views/devise/sessions
    create    app/views/devise/sessions/new.html.erb
    create    app/views/devise/unlocks
    create    app/views/devise/unlocks/new.html.erb
    invoke  erb
    create    app/views/devise/mailer
    create    app/views/devise/mailer/confirmation_instructions.html.erb
    create    app/views/devise/mailer/email_changed.html.erb
    create    app/views/devise/mailer/password_change.html.erb
    create    app/views/devise/mailer/reset_password_instructions.html.erb
    create    app/views/devise/mailer/unlock_instructions.html.erb

For example, the view on the login screen is app/views/devise/sessions/new.html.erb

The view on the sign-up screen is app/views/devise/registrations/new.html.erb.

You can edit these files to change the design.


This article was written with reference to the following information.

[* Rails *] How to use devise (rails5 version) Complete introduction to rails devise! What can devise do after all? [Rails] Let's master how to use devise and implement login authentication function!

Recommended Posts

[Rails] How to install devise
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use gem "devise"
[Rails] How to install Font Awesome
[Rails] How to use devise (Note)
[Rails] How to install ImageMagick (RMajick)
[Rails] How to install Font Awesome
How to install Swiper in Rails
How to write Rails
How to install Docker
How to uninstall Rails
How to install docker-machine
[Note] How to use Rails 6 Devise + cancancan
Super easy in 2 steps! How to install devise! !! (rails 5 version)
How to install MySQL
[Rails] How to translate devise into Japanese
How to install ngrok
[Ruby on Rails] How to install Bootstrap in Rails
[Rails] How to use enum
How to read rails routes
How to install Boots Faces
How to use rails join
(For beginners) [Rails] Install Devise
[Rails] Add column to devise
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
How to install JDK8-10 (Mac)
[Rails] How to use Scope
[Rails] devise
[Rails] How to edit and customize devise view and controller
[Rails] How to install a decorator using gem draper
How to deploy jQuery on Rails
Rails new fails to install mysql
[Rails] How to use flash messages
[rails] How to display db information
How to install JMeter for Mac
[Rails] How to prevent screen transition
How to install ImageMagick on Windows 10
How to install ruby through rbenv
[How to install Spring Data Jpa]
How to use Ruby on Rails
How to deploy Bootstrap on Rails
[Rails] How to speed up docker-compose
Super rough! How to install Dagger2
[Rails] How to add new pages
How to install Bootstrap in Ruby
How to install MariaDB 10.4 on CentOS 8
Rails on Tiles (how to write)
[Rails] How to write exception handling?
[Rails] How to use Active Storage
How to install WildFly on Ubuntu 18.04
[Rails] Add strong parameters to devise