Skip to main content

Single sign in Devise STI

Gems / Libraries • Asked by John Munyi

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

user.rb

    class User < ActiveRecord::Base

       #devise stuff here 
       has_many :attendances
        belongs_to :team

       self.inheritance_column = :type

        def self.types
        %w(Agent TeamLeader)
     end
 end

the sub classes

  class Agent < User

  end

  class TeamLeader< User

  end

sessioncontroller to allow single sign in

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

devise routes

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

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 %>

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.