[RUBY] [Rails] devise

The other day, I introduced devise with the implementation of login authentication, so I will keep a record.

What is devise

A gem package for introducing the login authentication function. You can easily implement sign-in, sign-out, sign-up (email authentication is possible), etc.

I will explain the actual usage.


Install devise. Add to Gemfile and execute bundle install.


gem 'devise'
$ bundle install

After the installation is complete, create the file. Running rails g devise: install will create the related files.

$ rails g devise:install

When executed, it will be displayed like this.

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

To summarize briefly

  1. URL setting
  2. Routing settings
  3. Display notifications and alerts
  4. You can copy the devise view file and customize it.

For the time being, let's display the notification and alert messages of 3. In ╩╗application.html.erb

<%= notice %>


<% = alert%> </ p>`.


<!DOCTYPE html>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>

    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

    <p class="notice"><%= notice %></p>
    <p class="alert"><%= alert %></p>
    <%= yield %>

Creating an authentication model

Create an authentication model and migration file with rails g devise model name.

$ rails g devise user

After creating the model and migration file, run rails db: migrate.

$ rails db:migrate

That's it. You can now use the authentication function using devise.

When editing the view file

$ rails g device:views

Doing this creates a view that you can edit as needed.

Recommended Posts

[Rails] devise
rails + devise + devise_token_auth
[Rails] devise helper method
[Rails] Customize devise validation
Handle devise with Rails
[Rails] devise introduction method
[rails] About devise defaults
[Rails] Introduction of devise Basics
What is Rails gem devise?
[Rails] gem devise installation flow
[Rails] How to install devise
(For beginners) [Rails] Install Devise
[Rails] Add column to devise
[rails] error during devise installation
Memorandum [Rails] User authentication Devise
[Rails] How to use gem "devise"
[Rails g.error]
Introducing devise
Rails basics
Rails Review 1
Rails API
[Rails] How to use devise (Note)
Rails migration
[Rails] Session timeout setting in devise
[rails] Login screen implementation in devise
rails tutorial
[Devise] rails memo basic setting initial setting
[Rails] Unexpected validation error in devise
About Rails 6
Rails foundation
Rails memorandum
rails tutorial
rails tutorial
rails tutorial
About devise
[Rails] [Devise] Edit profile without password
Ruby On Rails devise routing conflict
[Rails] Add strong parameters to devise
Devise procedure
Introducing devise
rails tutorial
Create My Page with Rails devise
rails tutorial
Rails Tips
rails method
rails tutorial
[Rails] ActiveRecord
[Rails] form_with
Rails Review 2
[Rails] Manage multiple models using devise gem
Rails g devise: install cannot be executed!
[Note] How to use Rails 6 Devise + cancancan
[Rails] How to translate devise into Japanese
Rails CSV basics
Rails6 jQuery introduced
Create authentication function in Rails application using devise
About Rails routing
Rails Routing Basics
Add binding.pry (rails)
Rails access restrictions
The road to Japaneseizing Rails devise error messages