我正在尝试为我的测验webapp编写功能,允许重新连接的用户继续他们离开的地方(重新连接的可能原因是:因为他们失去了连接,或者因为他们无意中退出,或者因为他们刷新页面。)
所以我用谷歌搜索并找到了解决方案,我可以在"after_sign_in_path_for(resource)"
中编写方法application_controller
,并且在用户登录后,我在该方法中放置的任何内容都将由Devise执行。
当我尝试渲染静态页面(甚至是许多静态页面中的一个,取决于我保存到我的数据库的“阶段”参数)时,此解决方案非常有效。 但我的应用程序有两种用户:
现在,当学生登录时会发生什么:
after_sign_in_path_for(resource)
:确定用户是学生并致电find_current_page
模型中的quiz_session
方法:它检查我的数据库中的phase
变量,并调用我频道服务器端的多种方法之一,例如。resend_question_page
中的IndividualUpdateChannel
类方法:此方法构建html并将其广播给学生。但是:不是接收和显示发送的HTML,而是学生得到“Devise中的NoMethodError :: SessionsController #create - 未定义的方法`student_url'用于#”。 在日志中它看起来像1.,2。和3.运行,但然后突然应用程序尝试将学生重定向到某个地方?并失败。
以下是控制台输出:https://pastebin.com/FKmHF4qS
...
[ActionCable] Broadcasting to student_#3: {:phase=>1, :answers=>["<div class='student-layout'>The call returns immediately without </br>waiting for the I/O to complete.</div>", {"ids"=>[9, 10, 11, 12], "current_question_index"=>1}]}
Redirected to
Completed 500 Internal Server Error in 442ms (ActiveRecord: 103.6ms)
这是完整的错误消息: 这是跟踪的其余部分:https://pastebin.com/psdyR2w2
以下是执行停止的代码:
#app\channels\individual_update_channel.rb:
class IndividualUpdateChannel < ApplicationCable::Channel
...
def self.resend_question_page(student)
quiz_session = student.quiz_session
new_answer_html = AnswerButtonCreation.new(quiz_session).create
#This is where it stops
ActionCable.server.broadcast("student_##{student.id}", phase: 1, answers: new_answer_html)
end
我正在使用Rails 5.0.2和Ruby 2.3.3和Devise 4.2.1。这是我的第一个Rails项目,所以请原谅我是否有一些错误的最佳实践。我在谷歌搜索Devise的内部工作并没有把我带到任何地方,所以我很感激任何帮助!
编辑: 这是我的路线档案:
Rails.application.routes.draw do
resources :given_answers
get 'students/set_quiz_session'
resources :given_answers
#change the sign_in and sign_out routes to /login and /logout
devise_for :users, path: '', path_names: { sign_in: 'login',
sign_out: 'logout'},
:controllers => {:registrations => 'registrations'}
resources :quiz_sessions, :answers, :questions, :quizzes, :courses
# Setup static pages
get 'teacher/quiz_start', to: 'pages#create_session', :as => :teacher_start_quiz
get 'teacher/quiz_question', to: 'pages#show_question', :as => :teacher_question
get 'teacher/quiz_question_result', to: 'pages#show_question_result', :as => :teacher_question_result
get 'teacher/quiz_result', to: 'pages#show_quiz_result', :as => :teacher_quiz_result
get 'teacher/home', to: 'pages#show', :role => "teacher", :page => "home", :as => :teacher_home
get 'student/quiz_start', to: 'pages#join_session', :as => :student_start_quiz
get 'student/home', :role => "student", :page => "home", :as => :student_home
get ':role/:page', to: 'pages#show'
root to: redirect('teacher/home'), :role => "teacher", :page => "home"
mount ActionCable.server => '/cable'
end