(For beginners) [Rails] Install Devise

Introduction

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

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

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

environment

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.

app/controllers/home_controller.rb


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

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

Recommended Posts

(For beginners) [Rails] Install Devise
[Rails] How to install devise
[Rails] devise
[Ruby on Rails] About bundler (for beginners)
Rails g devise: install cannot be executed!
[Rails] Introducing devise
Rails [For beginners] Implementation of comment function
rails + devise + devise_token_auth
Explanation of Ruby on rails for beginners ①
(For beginners) [Rails] Time saving tech! How to install and use slim
[Rails] Add page nation to table [For beginners]
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
Mac Rails Install Error
Handle devise with Rails
[Rails] devise introduction method
[rails] About devise defaults
Scraping for beginners (Ruby)
How to implement login request processing (Rails / for beginners)
About products using crud processing and devise (for beginners)
[Rails] Return login result in JSON format (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
Tutorial to create a blog with Rails for beginners Part 1
What is Rails gem devise?
Beginners install docker for mac and prepare php7.0 operating environment
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
[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
Memorandum [Rails] User authentication Devise
[Rails] How to display error messages for comment function (for beginners)
For beginners! Automatic deployment with Rails6 + CircleCI + Capistrano + AWS (EC2)
[RSpec on Rails] How to write test code for beginners by beginners
Ruby on Rails for beginners! !! Post list / detailed display function summary