Sunday, November 3, 2024
HomeRuby On RailsA number of Assets with Devise | Drifting Ruby

A number of Assets with Devise | Drifting Ruby


# Terminal
bundle add devise
rails g devise:set up
rails g devise Person
rails g devise Admin
rails g controller customers/dashboard index
rails g controller admins/dashboard index
rails g devise:controllers customers -c=classes
rails g devise:controllers admins -c=classes
rails g devise:controllers customers -c=registrations
rails g devise:controllers admins -c=registrations
# views/layouts/utility.html.erb
      <% if user_signed_in? %>
        <%= link_to 'Signal Out', destroy_user_session_path, methodology: :delete %> |
      <% elsif !admin_signed_in? %>
        <%= link_to "Signal In", new_user_session_path %> |
      <% finish %>

      <% if admin_signed_in? %>
        <%= link_to 'Signal Out as Admin', destroy_admin_session_path, methodology: :delete %> |
      <% elsif !user_signed_in? %>
        <%= link_to "Signal In as Admin", new_admin_session_path %> |
      <% finish %>
# config/routes.rb
Rails.utility.routes.draw do
  devise_for :customers, path: 'customers', controllers: {
    classes: 'customers/classes',
    registrations: 'customers/registrations'
  }

  devise_for :admins, path: 'admins', controllers: {
    classes: 'admins/classes',
    registrations: 'admins/registrations'
  }

  devise_scope :person do
    authenticated :person do
      namespace :customers do
        get 'dashboard/index', as: :authenticated_root
      finish
    finish
  finish

  devise_scope :admin do
    authenticated :admin do
      namespace :admins do
        get 'dashboard/index', as: :authenticated_root
      finish
    finish
  finish

  root to: 'welcome#index'
finish
# controllers/issues/accessible.rb
module Accessible
  prolong ActiveSupport::Concern
  included do
    before_action :check_resource
  finish

  protected

  def check_resource
    if admin_signed_in?
      flash.clear
      redirect_to(admins_authenticated_root_path) and return
    elsif user_signed_in?
      flash.clear
      redirect_to(users_authenticated_root_path) and return
    finish
  finish
finish
# controllers/admins/registrations_controller.rb
class Admins::RegistrationsController < Devise::RegistrationsController
  embody Accessible
  skip_before_action :check_resource, besides: [:new, :create]
finish
# controllers/admins/sessions_controller.rb
class Admins::SessionsController < Devise::SessionsController
  embody Accessible
  skip_before_action :check_resource, solely: :destroy
finish
# controllers/customers/registrations_controller.rb
class Customers::RegistrationsController < Devise::RegistrationsController
  embody Accessible
  skip_before_action :check_resource, besides: [:new, :create]
finish
# controllers/customers/sessions_controller.rb
class Customers::SessionsController < Devise::SessionsController
  embody Accessible
  skip_before_action :check_resource, solely: :destroy
finish
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments