2016-03-30 5 views
0

私は学校ERPに取り組んでいます。開発者のログインユーザーは、admin(機関)を作成し、admin(機関)は(教師、学生)を作成します。認証管理教師生徒所属機関

私は各機関(自分のログインが )に別々のURLを生成したいと思いますし、各機関には3つのロール (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 

私はレールが新しく、これに驚いています。 : '(事前に感謝

ルート/ institution_id - >ログインページのドロップダウン(管理者、教師、学生)と.. thse THREは、クエリがユーザーにinstituion_idを経由して行くべきinstituionに属し ください

感謝。。。それDINT認証がリダイレクトされるようにトレーニング

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


class Institution< ActiveRecord::Base 
    has_many :teachers 
end 

teachermodel

:予め!!!

iが工夫とfind_by_id([ID]のparams)三モデルの作成を試みました

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' 
+0

は、あなたの作成したコードを貼り付けることができますしてみてください? – SpunkyLive

+0

ありがとう、コードを貼り付けた – geekghost

+0

@Asviniprは正しいですが、私は別の役割を使用して単一のテーブルでそれをやりたいと思います。そして、役割はcancancanによって管理することができます。 – SpunkyLive

答えて

0

別個のログインのために認証されたルートを提供するように工夫する。 例:

この