[Ruby] (For beginners) [Rails] Installing Devise

1 minute read

Introduction

Hello! It is Yori-goreng who is learning Rails: sunny: In this article, I will introduce the devise that controls the login function and the user’s permission: relaxed:

For more details, please see the following reference articles.

  • Reference article

https://qiita.com/cigalecigales/items/f4274088f20832252374

https://qiita.com/tobita0000/items/866de191635e6d74e392

Environment

  • ruby 2.6.3
  • rails 5.2.4.3
  • MacOS Catalina version 10.15.4

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

1. [Preparing the app]

1.1. Application creation

Create an app with rails new app name. Here, let’s call it devise_test.

rails new devise_test
cd devise_test

1.2. DB creation

rails db:create

2. [Preparation for devise]

2.1. Installation of gem

Add the devise gem to the gemfile.

gem'devise'

Install the gem.

bundle install

2.2. Create devise related files

rails g devise:install

If the following long message is displayed on the terminal, it is 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. Model creation

Create a User model with the following command.

rails g devise user

Running the migration at this point will complete a simple login page.

rails db:migrate
rails s

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

Screenshot 2020-06-15 12.02.35.png

2.4. before_action :authenticate_user!

If you describe before_action :authenticate_user! in the controller, the process performed here can be executed only by the logged-in user. As an example, create a Homes controller. *There is a plural naming convention for controllers such as homes. It seems that there is no problem in freely choosing the name of the controller.

rails g controller homes index

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

app/controllers/home_controller.rb


class HomesController <ApplicationController
  before_action :authenticate_user!
  def index
  end
end

With this, only the logged-in user can check the list display by the index action.