认证管理员教师学生属于机构

时间:2016-03-30 05:30:03

标签: ruby-on-rails authentication devise cancan authlogic

我正在研究ERP。开发人员登录用户创建管理员(机构)和管理员(机构)创建(教师,学生)。

我想为每个机构(有自己的机构)生成单独的网址 自己登录)每个机构都有三个角色 (管理员,教师,学生)。

 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

我是rails的新手,我很震惊。 :'(。提前致谢。

root / institution_id - >登录页面带下拉列表(管理员,老师,学生)..这些属于机构查询应该通过instituion_id到用户。 请

提前致谢!!!

我试过在devise和find_by_id(params [:id])中创建三个模型,它在Dint exercise中作为身份验证重定向。

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

路由

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 个答案:

答案 0 :(得分:0)

设计为单独登录提供经过身份验证的路由。 例如

     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

试试这个