Ask A Question

Notifications

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

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 %>
Reply
Join the discussion
Create an account Log in

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

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

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