Implement the authentication process for each user and administrator using devise. The namespace is implemented without dividing it so that the explanations unrelated to devise do not increase, but it seems that it is often better to separate it when actually developing from 0.
See the following article for the basic usage of devise [Rails] Key points for using devise at a minimum --Qiita
Gemfile
gem 'devise'
$ bundle install
$ rails g devise:install
config/environments/development.rb
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
erb:app/views/layouts/application.html.erb
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
$ rails g devise User
$ rails g devise AdminUser
$ rails db:migrate
$ rails g devise:controllers users
$ rails g devise:controllers admin_users
config/initializers/devise.rb
config.scoped_views = true
$ rails g devise:views users
$ rails g devise:views admin_users
config.routes.rb
Rails.application.routes.draw do
devise_for :users, controllers: {
sessions: 'users/sessions',
passwords: 'users/passwords',
registrations: 'users/registrations'
}
devise_for :admin_users, controllers: {
sessions: 'admin_users/sessions',
passwords: 'admin_users/passwords',
registrations: 'admin_users/registrations'
}
end
at this point http://localhost:3000/users/sign_in http://localhost:3000/admin_users/sign_in Access and confirm that the login screen is displayed.
$ rails g controller static_pages index
$ rails g scaffold UserPost title body
$ rails g scaffold AdminUserPost title body
$ rails db:migrate
config/routes.rb
Rails.application.routes.draw do
root to: 'static_pages#index'
resources :admin_user_posts
resources :user_posts
#abridgement
end
erb:app/views/static_pages/index.html.erb
<h2>user</h2>
<%= link_to 'Login', new_user_session_path %>
<%= link_to 'sign up', new_user_registration_path %>
<h2>Administrator</h2>
<%= link_to 'Login', new_admin_user_session_path %>
<%= link_to 'sign up', new_admin_user_registration_path %>
erb:app/views/user_posts/index.html.erb
<%= link_to 'Log out', destroy_user_session_path, method: :delete %>
.
.
.
erb:app/views/admin_user_posts/index.html.erb
<%= link_to 'Log out', destroy_admin_user_session_path, method: :delete %>
.
.
.
After logging in as User, go to user_posts # index
Set to transition to admin_user_posts # index
after logging in as AdminUser.
app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
def after_sign_in_path_for(resource)
case resource
when User
user_posts_path
when AdminUser
admin_user_posts_path
end
end
end
The User Post screen can be displayed only when logged in as User You can display the AdminUserPost screen only when you are logged in as AdminUser Set as.
app/controllers/user_posts_controller.rb
class UserPostsController < ApplicationController
before_action :authenticate_user!
#abridgement
end
app/controllers/admin_user_posts_controller.rb
class AdminUserPostsController < ApplicationController
before_action :authenticate_admin_user!
#abridgement
end
This time, there is only one controller for both the user screen and the management screen, so there is no problem with this method, but in reality, multiple controllers will be created, so name_space is separated and each application_controller
is set to before_action
. I think it's better.
Recommended Posts