All threads / Single sign in Devise STI

Ask A Question


You’re not receiving notifications from this thread.

Single sign in Devise STI

John Munyi asked in Gems / Libraries

Hey there I have an STI on devise model which is working but there is an issue with how to manage what menus to show when different types of users are logged in here is my code


    class User < ActiveRecord::Base

       #devise stuff here 
       has_many :attendances
        belongs_to :team

       self.inheritance_column = :type

        def self.types
        %w(Agent TeamLeader)

the sub classes

  class Agent < User


  class TeamLeader< User


sessioncontroller to allow single sign in

  class SessionsController < Devise::SessionsController
      def create
         rtn = super
         sign_in(resource.type.underscore, resource.type.constantize.send(:find, unless resource.type.nil?

devise routes

   Rails.application.routes.draw do
      devise_for :users, :controllers => { :sessions => 'sessions' }
      devise_for :team_leaders, :skip => :sessions
     devise_for :agents, :skip => :sessions

so if i try to pass a view with this code , <% elsif team_leader_signed_in? %> and <% elsif agent_signed_in? %> get totally ignored and the links are not displayed, what I am missing

       <% if user_signed_in? %>
          <li><%= link_to "Account", edit_user_registration_path %></li>
         <li><%= link_to "Log out", destroy_user_session_path, method: :delete %></li>
      <% elsif team_leader_signed_in? %>
          <li><%= link_to "I am Admin" %></li>
      <% elsif agent_signed_in? %>
         <li><%= link_to "I am Agent" %></li>
     <% else %>
         <li><%= link_to "Sign in",  new_user_session_path %></li>
         <li><%= link_to "Sign up",  new_user_registration_path %></li>
     <% end %>
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 53,130+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2021 GoRails, LLC. All rights reserved.