Search code examples
ruby-on-railsruby-on-rails-4devisedevise-invitable

How to set the Inviter using devise_inevitable gem in rails 4?


I am working on rails 4 and setup devise_invitable gem recently. I have a Student and Teachers models in which Student get invited by Teacher means Teacher is Inviter and Student is Invitee.

I have added all necessary columns in students table like this:

    t.string   "invitation_token"
    t.datetime "invitation_created_at"
    t.datetime "invitation_sent_at"
    t.datetime "invitation_accepted_at"
    t.integer  "invitation_limit"
    t.integer  "invited_by_id"
    t.string   "invited_by_type"
    t.integer  "invitations_count",      default: 0

I login through Teachers account and now want to invite a student, when I click on link to invite a student then it goes to path: "/students/invitation/new" and request me to login as a student which does't make sense because I want to invite student then why do I need to login as student even Teacher session is already there and it should use teacher object to invite student.

I even try below code but it does't work:

class Teacher < ActiveRecord::Base
     include DeviseInvitable::Inviter
  end

Here are my routes:

                    Prefix Verb   URI Pattern                           Controller#Action
        new_student_session GET    /students/login(.:format)             devise/sessions#new
            student_session POST   /students/login(.:format)             devise/sessions#create
    destroy_student_session DELETE /students/logout(.:format)            devise/sessions#destroy
           student_password POST   /students/password(.:format)          devise/passwords#create
       new_student_password GET    /students/password/new(.:format)      devise/passwords#new
      edit_student_password GET    /students/password/edit(.:format)     devise/passwords#edit
                            PATCH  /students/password(.:format)          devise/passwords#update
                            PUT    /students/password(.:format)          devise/passwords#update
cancel_student_registration GET    /students/cancel(.:format)            devise_invitable/registrations#cancel
       student_registration POST   /students(.:format)                   devise_invitable/registrations#create
   new_student_registration GET    /students/sign_up(.:format)           devise_invitable/registrations#new
  edit_student_registration GET    /students/edit(.:format)              devise_invitable/registrations#edit
                            PATCH  /students(.:format)                   devise_invitable/registrations#update
                            PUT    /students(.:format)                   devise_invitable/registrations#update
                            DELETE /students(.:format)                   devise_invitable/registrations#destroy
  accept_student_invitation GET    /students/invitation/accept(.:format) devise/invitations#edit
  remove_student_invitation GET    /students/invitation/remove(.:format) devise/invitations#destroy
         student_invitation POST   /students/invitation(.:format)        devise/invitations#create
     new_student_invitation GET    /students/invitation/new(.:format)    devise/invitations#new
                            PATCH  /students/invitation(.:format)        devise/invitations#update
                            PUT    /students/invitation(.:format)        devise/invitations#update
        new_teacher_session GET    /teachers/login(.:format)             devise/sessions#new
            teacher_session POST   /teachers/login(.:format)             devise/sessions#create
    destroy_teacher_session DELETE /teachers/logout(.:format)            devise/sessions#destroy
           teacher_password POST   /teachers/password(.:format)          devise/passwords#create
       new_teacher_password GET    /teachers/password/new(.:format)      devise/passwords#new
      edit_teacher_password GET    /teachers/password/edit(.:format)     devise/passwords#edit
                            PATCH  /teachers/password(.:format)          devise/passwords#update
                            PUT    /teachers/password(.:format)          devise/passwords#update
cancel_teacher_registration GET    /teachers/cancel(.:format)            devise_invitable/registrations#cancel
       teacher_registration POST   /teachers(.:format)                   devise_invitable/registrations#create
   new_teacher_registration GET    /teachers/sign_up(.:format)           devise_invitable/registrations#new
  edit_teacher_registration GET    /teachers/edit(.:format)              devise_invitable/registrations#edit
                            PATCH  /teachers(.:format)                   devise_invitable/registrations#update
                            PUT    /teachers(.:format)                   devise_invitable/registrations#update
                            DELETE /teachers(.:format)                   devise_invitable/registrations#destroy
           new_user_session GET    /users/login(.:format)                devise/sessions#new
               user_session POST   /users/login(.:format)                devise/sessions#create
       destroy_user_session DELETE /users/logout(.:format)               devise/sessions#destroy
              user_password POST   /users/password(.:format)             devise/passwords#create
          new_user_password GET    /users/password/new(.:format)         devise/passwords#new
         edit_user_password GET    /users/password/edit(.:format)        devise/passwords#edit
                            PATCH  /users/password(.:format)             devise/passwords#update
                            PUT    /users/password(.:format)             devise/passwords#update
   cancel_user_registration GET    /users/cancel(.:format)               devise_invitable/registrations#cancel
          user_registration POST   /users(.:format)                      devise_invitable/registrations#create
      new_user_registration GET    /users/sign_up(.:format)              devise_invitable/registrations#new
     edit_user_registration GET    /users/edit(.:format)                 devise_invitable/registrations#edit
                            PATCH  /users(.:format)                      devise_invitable/registrations#update
                            PUT    /users(.:format)                      devise_invitable/registrations#update
                            DELETE /users(.:format)                      devise_invitable/registrations#destroy
          static_pages_home GET    /static_pages/home(.:format)          static_pages#home
         dashboard_teachers GET    /teachers/dashboard(.:format)         teachers#dashboard
                       root GET    /                                     static_pages#home

routes.rb

Rails.application.routes.draw do

  devise_for :students, path_names: {sign_in: "login", sign_out: "logout"}
  devise_for :teachers, path_names: {sign_in: "login", sign_out: "logout"}
  devise_for :users, path_names: {sign_in: "login", sign_out: "logout"}
  get 'static_pages/home'

  resources :teachers, only: [] do 
  collection do
    get :dashboard
  end
  end  


   root 'static_pages#home'

end

Thanking you in anticipation.


Solution

  • Override authenticate_inviter method of gem in Application Controller:

    class ApplicationController < ActionController::Base
    protected
      def authenticate_inviter!
        authenticate_teacher!(:force => true)
      end
    end
    

    And include DeviseInvitable::Inviter module into Teacher model:

    class Teacher < ActiveRecord::Base
      devise :database_authenticatable, :validatable
      include DeviseInvitable::Inviter
    end
    

    You can define association in model by which you want that all invitations should be sent by this:

    class Teacher < ActiveRecord::Base
        has_many :invitations, :class_name => self.to_s, :as => :invited_by
    end