我正在使用rails 4并最近设置devise_invitable gem。我有一个学生和教师模型,其中学生受教师的邀请意味着教师是邀请者,学生是受邀者。
我在学生表中添加了所有必要的列,如下所示:
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
我通过教师帐户登录,现在想邀请学生,当我点击链接邀请学生时,它会转到路径:" / students / invitation / new"并要求我作为一个有意义的学生登录因为我想邀请学生然后为什么我需要以学生身份登录甚至教师会话已经存在并且应该使用教师对象来邀请学生。
我甚至尝试下面的代码,但它确实无效:
class Teacher < ActiveRecord::Base
include DeviseInvitable::Inviter
end
以下是我的路线:
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
在期待中感谢你。
答案 0 :(得分:0)
不幸的是,我对你的问题发表评论缺少7个声望点。所以我这样做。
你有两个用户,1)学生2)老师。为什么有3种登录方式?
无论如何,我猜你只希望老师能够向学生发送邀请。正如我现在所看到的那样,你制作了自己的路线,只有学生才能向学生发送邀请。那么为什么不改变路线呢?
答案 1 :(得分:0)
在Application Controller中覆盖gem的authenticate_inviter方法:
class ApplicationController < ActionController::Base
protected
def authenticate_inviter!
authenticate_teacher!(:force => true)
end
end
将DeviseInvitable :: Inviter模块包含在教师模型中:
class Teacher < ActiveRecord::Base
devise :database_authenticatable, :validatable
include DeviseInvitable::Inviter
end
您可以在模型中定义您希望通过此方式发送所有邀请的关联:
class Teacher < ActiveRecord::Base
has_many :invitations, :class_name => self.to_s, :as => :invited_by
end