(2021) Ruby on Rails administrator (admin) login creation

Prerequisites

Terminal


$ rails g devise Admin
$ rails db:migrate

Create a user to log in as admin.

db/seeds.rb


Admin.create!(
   email: 'admin@admin',
   password: 'testtest'
)

Terminal


$ rails db:seed

Check if the admin user data is included.

Terminal


$ rails c
$ irb(main):001:0> Admin.all
$ irb(main):002:0> exit

Next, create the view and controller.

Terminal


$ rails g devise:views admins
$ rails g devise:controllers admins

Edit the routing.

config/routes.rb


devise_for :admins
↓
devise_for :admins, controllers: {
  sessions: 'admins/sessions'
}

Add a header screen after login.

:application.html.erb


<% if customer_signed_in? %>
 <li>
  <%= link_to "Log out", destroy_customer_session_path, method: :delete %>
 </li>
<% elsif admin_signed_in? %>
 <li>
  <%= link_to "Log out", destroy_admin_session_path, method: :delete %>
 </li>
<% else %>
 <li>
  <%= link_to "sign up", new_customer_registration_path %>
 </li>
 <li>
  <%= link_to "Login", new_customer_session_path %>
 </li>
<% end %>

After logging in as an administrator, create a page transition and log-out destination.

controllers/admins/sessions_contoller.rb


protected
  def after_sign_in_path_for(resource)
    admin_items_path
  end

  def after_sign_out_path_for(resource)
    new_admin_session_path
  end

Recommended Posts

(2021) Ruby on Rails administrator (admin) login creation
Portfolio creation Ruby on Rails
Ruby on Rails application new creation command
Ruby on Rails Elementary
Ruby on Rails basics
Ruby On Rails Association
Validation settings for Ruby on Rails login function
[Beginner Procedure Manual 2] Ruby on Rails: Rails template creation
Ruby on Rails <2021> Implementation of simple login function (form_with)
Implementation of Ruby on Rails login function (devise edition)
Ruby on rails learning record -2020.10.04
[Ruby on Rails] Debug (binding.pry)
Ruby on rails learning record -2020.10.05
Ruby on Rails basic learning ①
[Ruby on Rails] about has_secure_password
Ruby on rails learning record-2020.10.07 ②
Commentary on partial! --Ruby on Rails
Ruby on rails learning record-2020.10.07 ①
Cancel Ruby on Rails migration
Ruby on rails learning record -2020.10.06
Explanation of Ruby on rails for beginners ⑥ ~ Creation of validation ~
Ruby on Rails validation summary
Ruby on Rails Basic Memorandum
[Ruby on Rails] Implement login function by add_token_to_users with API
[Apple login] Sign in with Apple implementation procedure (Ruby on Rails)
Ruby on Rails Overview (Beginner Summary)
[Ruby on Rails] Read try (: [],: key)
Installing Ruby + Rails on Ubuntu 18.04 (rbenv)
[Ruby on Rails] Introduced paging function
Basic knowledge of Ruby on Rails
Progate Ruby on Rails5 Looking Back
How to use Ruby on Rails
[Ruby on Rails] Add / Remove Columns
Ruby on Rails Japanese-English support i18n
[Ruby on Rails] CSV output function
Ruby on Rails 6.0 environment construction memo
[Ruby on Rails] What is Bcrypt?
Ruby On Rails devise routing conflict
[Ruby on Rails] Comment function implementation
[Ruby on Rails] DM, chat function
[Ruby on Rails] Convenient helper method
[Ruby on Rails] Stop "looping until ..."
[Ruby on Rails] Introduction of initial data
[Ruby on Rails] Search function (not selected)
[Rails] Addition of Ruby On Rails comment function
[Ruby on Rails] Creating an inquiry form
Ruby on Rails6 Practical Guide cp13 ~ cp15 [Memo]
[Ruby on Rails] View test with RSpec
[Ruby on Rails] Code check using Rubocop-airbnb
[Ruby on Rails] 1 model CRUD (Routing Main)
Ruby on Rails installation method [Mac edition]
[Ruby on Rails] model, controller terminal command
Let's summarize "MVC" of Ruby on Rails
[Ruby on Rails] About bundler (for beginners)
part of the syntax of ruby ​​on rails
Ruby on Rails6 Practical Guide cp7 ~ cp9 [Memo]
Ruby on Rails in Visual Studio Codespaces
[Ruby on Rails] Follow function implementation: Bidirectional
Notes on using FCM with Ruby on Rails
[Ruby on Rails] Controller test with RSpec
Deploy to Heroku [Ruby on Rails] Beginner