Super easy in 2 steps! How to install devise! !! (rails 5 version)

Hello! In this first post, I'll show you how to implement the login feature using devise. I would like to explain the flow from installation to actual display and login.

What is devise

What is devise in the first place? devise is a gem that can easily implement login function, user registration function, etc. In the initial state, you can log in and sign up by setting ** email and password **. It is a gem that you definitely want to use when you want to implement the membership function!

Step 1: Install devise

First, let's install devise.

Write "gem'devise'" at the bottom of the Gemfile.

Gemfile


gem 'devise'

Let the application load the added gem. Type the following in the terminal.

$ bundle install

Then make the initial settings for devise.

$ rails g devise:install

Installation is successful if the following message is displayed! スクリーンショット 2020-06-11 17.47.28.png

Step 2: Display the login screen

Now let's actually display the login screen. This time, we will create a user table in the database to store user information. The table name is "User". Type the following into your terminal:

$ rails g devise User

Originally, if you want to create a table from scratch, you need to create a column along with the table, but if you use devise, ** user table and column will be created automatically **. Let's check the migration file that was actually created.

db/migrate/(Year, month, day, hour, minute, second)_devise_create_users.rb


class DeviseCreateUsers < ActiveRecord::Migration[5.2]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: "" #mail address
      t.string :encrypted_password, null: false, default: "" #password

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

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end

The columns required for the login function are an email address and password. The email address is on the line t.string: email, ……, and the password is on the line t.string: encrypted_password, …….

Once confirmed, migrate this file to the database.

$ rails db:migrate

If you do so far, you are ready! Finally, let's display it in the browser.

Start the server with rails s and enter ** "/ users / sign_in" ** after the URL to open the login screen. スクリーンショット 2020-06-11 19.08.47.png It should look like this. Then enter ** "/ users / sign_up" ** after the URL to open the sign-up screen. スクリーンショット 2020-06-11 19.10.43.png

If this display is displayed, it is complete. After that, let's rewrite as you like and create an original site!   Until the end Thank you for reading! I would like to write more detailed articles and advanced contents in the future, so I would be grateful if you could read it. I also do Twitter. We also send out daily tweets and information on rare occasions, so please follow us ☺️ → @saketi_sei

Recommended Posts

Super easy in 2 steps! How to install devise! !! (rails 5 version)
How to install Swiper in Rails
[Ruby on Rails] How to install Bootstrap in Rails
[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)
Super rough! How to install Dagger2
How to install Bootstrap in Ruby
[Rails] How to install Font Awesome
How to introduce jQuery in Rails 6
[Webpacker] Summary of how to install Bootstrap and jQuery in Rails 6.0
I want to add devise in Rails, but I can't bundle install
[Rails] How to reset the database in production environment (Capistrano version)
How to change app name in rails
How to use custom helpers in rails
How to blur an image (super easy)
How to insert a video in Rails
How to use MySQL in Rails tutorial
Steps to set a favicon in Rails
[rails] How to configure routing in resources
How to install the legacy version [Java]
How to implement ranking functionality in Rails
[Note] How to use Rails 6 Devise + cancancan
How to use credentials.yml.enc introduced in Rails 5.2
[Rails] How to translate devise into Japanese
[Rails] How to log in with a name by adding a devise name column
How to update user edits in Rails Devise without entering a password
[Rails] How to get the user information currently logged in with devise
[Rails] How to use select boxes in Ransack
How to install Titan2D (v4.2.0) in virtual environment
How to translate Rails into Japanese in general
How to prevent direct URL typing in Rails
How to separate .scss by controller in Rails
How to implement a like feature in Rails
How to use JQuery in js.erb of Rails6
How to Install Oracle JDK 1.8 in Ubuntu 18.04 LTS?
[Rails] Added in devise: username not added to database
How to make a follow function in Rails
[Rails] How to use PostgreSQL in Vagrant environment
How to check Rails commands in the terminal
How to write Rails
How to install Docker
How to install MySQL
How to install ngrok
How to set the display time to Japan time in Rails
How to implement guest login in 5 minutes in rails portfolio
How to implement a like feature in Ajax in Rails
[Ruby on Rails] How to write enum in Japanese
Introduce devise in Rails to implement user management functionality
[Ruby On Rails] How to reset DB in Heroku
[How to insert a video in haml with Rails]
How to write a date comparison search in Rails
How to query Array in jsonb with Rails + postgres
[Rails 6] How to set a background image in Rails [CSS]
Install MySQL 5.6 on CentOS6 [How to specify the version]
[Rails] How to load JavaScript in a specific view
[Rails] How to display an image in the view
[Rails] How to install a decorator using gem draper
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]