0
votes

I am working on school ERP. Developer login user creates admin (institution) and admin (institution) creates (teacher, student).

I want to generate seperate url for each institution (which has their own login ) and each instituion have three roles (admin,teacher,student).

 class Institute
   has_many :institute_admins
   has_many :students
   has_many :teacher
 end
 class InstituteAdmin
   belongs_to :institute
 end
 class Student
   belongs_to :institute
 end
 class Teacher
   belongs_to :institute
 end

I am new to rails, and I am struck with this . :'( . Thanks in advance.

root/institution_id -> login page with dropdown (admin,teacher,student)..thse thre belongs to instituion query should go via instituion_id to users . please

thanks in advance !!!

i have tried with creating three models in devise and find_by_id(params[:id]) , it Dint workout As authentication redirected .

rails generate devise InstituteAdmin
rails generate devise Student
rails generate devise Teacher


class Institution< ActiveRecord::Base
  has_many :teachers
end

teachermodel

class Teacher< ActiveRecord::Base
  devise :database_authenticatable,
     :recoverable, :rememberable, :trackable, :validatable
     belongs_to :institute
     accepts_nested_attributes_for :institution
     validates_presence_of :institute_id
end

TeachersController

class TeachersController < ApplicationController
  def index
  end

  def new
    @teacher = Teacher.new
  end

  def create
    @teacher = current_institute.teachers.build(teacher_params)
    if @teacher.save
      flash[:notice] = "Teacher Created!"
      redirect_to(:controller => 'institutes', :action => 'show', :id => current_institute.id)
    else
      flash[:alert] = 'Mistake in the Teacher Creation!'
      render('new')
    end
  end

  private

  def teacher_params
    params.require(:teacher).permit(:email, :password, :institute_id)
  end
end

admincontroller

class AdminsController < ApplicationController
  before_action :authenticate_institute!, except: 'index'

  def show
    @institute = Institute.find_by_id(params[:id])
    @teacher = @institute.teachers
  end
end

adminmodel

class Institute < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable,
         :recoverable, :rememberable, :trackable, :validatable

         has_many :admins
         has_many :teachers
         has_many :students

         def to_param
           'institutes'
         end
end

routes

devise_for :students
  devise_for :teachers
  devise_for :admins
   resources :teachers
  resources :students

  # root 'institutes#index'
  get "/institutes" => "institutes#index", :as => :root

  get '/institutes/:id' => 'institutes#show'
1
Can you paste your create code ?Anuj
Thanks , pasted the codegeekghost
@Asvinipr is right, but I would like to do it with a single table with using different roles. And roles can be managed by cancancan.Anuj
i'm struggling with root/institute_id which has login , where every user belongs_to institution..ie cannot authenticate user by institution_idgeekghost

1 Answers

0
votes

Devise providing the authenticated routes for the separate login . Eg

     authenticated :user do
       root :to => 'admin/users#index', as: "authenticated_user_root"
     end

    authenticated :auditor do
     root :to => "auditors#index", as: "authenticated_auditor_root"
    end

try this