(For beginners) [Rails] Install Devise


Hello! This is Yori-goreng who is learning Rails: sunny: In this article, we will introduce the introduction of devise, which controls the login function, and user permissions: relaxed:

If you want to know more details, please see the following reference articles.

--Reference article




What is devise

It is one of the Ruby gems, and you can easily implement the login function by using devise.

1. [Preparation of application]

1.1. Creating an app

Create an app with rails new app name. For now, let's name it devise_test.

rails new devise_test
cd devise_test

1.2. DB creation

rails db:create

2. [Preparation for devise]

2.1. Install gem

Add devise gem to gemfile.

gem 'devise'

Install gem.

bundle install

2.2. Create devise related files

rails g devise:install

If you see a long sentence like the one below in your terminal, you are successful.


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


2.3. Modeling

Create a User model with the following command.

rails g devise user

Performing a migration at this point completes a simple login page.

rails db:migrate
rails s

After rails s, open the login page at http: // localhost: 3000 / users / sign_in.

スクリーンショット 2020-06-15 12.02.35.png

2.4. before_action :authenticate_user! If you write before_action: authenticate_user! in the controller, the processing performed here can be executed only by the logged-in user. As an example, create a Homes controller.

rails g controller homes index

Add before_action: authenticate_user! to the created homes_controller.rb.


class HomesController < ApplicationController
  before_action :authenticate_user!
  def index

This allows only the logged-in user to see the list display by the index action.

Recommended Posts

(For beginners) [Rails] Install Devise
[Rails] devise
[Rails] Introducing devise
Rails [For beginners] Implementation of comment function
Explanation of Ruby on rails for beginners ①
(For beginners) [Rails] Time saving tech! How to install and use slim
Use [Rails] devise Guest user function (for portfolio)
[For beginners] Test devise user registration with RSpec
[For beginners] Procedure for creating a controller using rails
[Rails] devise helper method
[Rails] Customize devise validation
[For rails beginners] Specify transition destination after logging in to multiple Devise models
Handle devise with Rails
[Rails] devise introduction method
[rails] About devise defaults
About products using crud processing and devise (for beginners)
[For Rails beginners] Implemented multiple search function without Gem
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Explanation of Ruby on rails for beginners ② ~ Creating links ~
Explanation of Ruby on rails for beginners ⑦ ~ Flash implementation ~
[Rails] Introduction of devise Basics
[For beginners] Install JDK + Eclipse on Mac (without Pleiades All-in-One)
docker-compose command list (for Rails)
[Rails] gem devise installation flow
[Rails] Workaround for classes automatically generated by devise error messages
Java debug execution [for Java beginners]
[Java] Basic statement for beginners
Install latest git for Debian10
[Procedure 1 for beginners] Ruby on Rails: Construction of development environment
Preparation for developing with Rails
[For super beginners] DBUnit super introduction
[For super beginners] Ant super introduction
Install Rails on macOS Catalina
More usable Enumerable for beginners
[Rails] Add column to devise
Java for beginners, data hiding
Super easy in 2 steps! How to install devise! !! (rails 5 version)
Explanation of Ruby on rails for beginners ③ ~ Creating a database ~
Tutorial to create a blog with Rails for beginners Part 2
Install docker, docker-compose for debin10
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[For super beginners] Maven super introduction
[rails] error during devise installation
Java application for beginners: stream
Tutorial to create a blog with Rails for beginners Part 0
[RSpec on Rails] How to write test code for beginners by beginners
Explanation of Ruby on rails for beginners ⑤ ~ Edit and delete database ~
I want to add devise in Rails, but I can't bundle install
[Ruby on Rails] How to avoid creating unnecessary routes for devise
Binary Tree Memorandum for Beginners (Part 1)
[Rails] How to use devise (Note)
[For beginners] Summary of java constructor
How to install JMeter for Mac
[Rails] Session timeout setting in devise
[Rails] Introduction of Rubocop by beginners
[rails] Login screen implementation in devise
Rock-paper-scissors game for beginners in Java
Environment construction with Docker for beginners
[Devise] rails memo basic setting initial setting
Java for beginners, expressions and operators 1