【发布时间】:2017-10-26 17:11:15
【问题描述】:
我正在尝试使用 2 种用户类型和这个用例制作一个测验应用程序(非单页应用程序):
- 测验主持人用户和参与者用户正在“等待 page",直到测验开始
- 测验主持人用户单击“测验开始”按钮开始测验
- 这会立即强制所有参与者用户重定向到带有测验问题的新页面
我按照教程在我的应用中设置了 ActionCable,但我想知道如何实现第 3 步。我当前频道的咖啡脚本文件如下所示:
# \app\assets\javascripts\channels\quiz_data.coffee:
App.quiz_data = App.cable.subscriptions.create "QuizDataChannel",
connected: ->
# Called when the subscription is ready for use on the server
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
# Called when there's incoming data on the websocket for this channel
if current_student
# HERE I want to render the page "quiz_question.html.erb"
# ...and then I want to append the answer buttons:
$('answer-buttons').append(data.partial)
send_data: ->
@perform 'send_data'
我很确定答案很简单,但是我在 Google 上搜索了很多 Coffeescript 和 ActionCable 教程,几乎所有这些教程都只将部分内容渲染到用户已经在的页面上。我是 Rails 初学者,对 Coffeescript 一无所知,因此我们将不胜感激!
编辑:
这是我尝试遵循 Laiths 回答后的咖啡脚本文件的样子:
App.quiz_data = App.cable.subscriptions.create "QuizDataChannel",
connected: ->
# Called when the subscription is ready for use on the server
$('#btn btn-primary btn-lg').on 'click', (e) -> App.quiz_data.send_data()
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
# Called when there's incoming data on the websocket for this channel
if current_student
pageHtml = data.page_html
answerHtml = data.answer_html
# This will replace your body with the page html string:
$('body').html(pageHtml)
# Then add your answer buttons:
#$('#answer-buttons').html(answerHtml)
$('answer-buttons').append(answerHtml)
send_data: ->
@perform 'send_data'
这是我创建的用于渲染部分片段并广播它们的作业:
# app\assets\jobs\quiz_data_broadcast_job.rb:
class QuizDataBroadcastJob < ApplicationJob
queue_as :default
def perform(answers)
ActionCable.server.broadcast('quiz_data', {
page_html: render_page,
answer_html: render_answer_options(answers)
})
end
private
def render_page
ApplicationController.render(
partial: 'pages/student/quiz_question',
locals: {}
)
end
def render_answer_options(answers)
answers.each do |answer|
ApplicationController.render(
#render student/quiz_question page and render as many answer_option partials as needed
partial: 'pages/student/answer_option',
locals: {answer: answer}
)
end
end
end
编辑 2:
这是我的 Javascript 控制台所说的:
Uncaught ReferenceError: App is not defined
at quiz_data.self-21fd077347e9c34e83bab1a2d43a8db5b083fff7ed4eaa02e5314aa78f1dba8b.js:2
at quiz_data.self-21fd077347e9c34e83bab1a2d43a8db5b083fff7ed4eaa02e5314aa78f1dba8b.js:23
这就是我点击它时显示的内容:
1 (function() {
2 App.quiz_data = App.cable.subscriptions.create("QuizDataChannel", {
3 connected: function() {
4 return $('#btn btn-primary btn-lg').on('click', function(e) {
5 return App.quiz_data.send_data();
6 });
7 },
8 disconnected: function() {},
9 received: function(data) {
10 var answerHtml, pageHtml;
11 if (current_student) {
12 pageHtml = data.page_html;
13 answerHtml = data.answer_html;
14 $('body').html(pageHtml);
15 return $('answer-buttons').append(answerHtml);
16 }
17 },
18 send_data: function() {
19 return this.perform('send_data');
20 }
21 });
22
23 }).call(this);
编辑 3: 这是我的 cable.js:
// Action Cable provides the framework to deal with WebSockets in Rails.
// You can generate new channels where WebSocket features live using the rails generate channel command.
//
//= require action_cable
//= require_self
//= require_tree ./channels
(function() {
this.App || (this.App = {});
App.cable = ActionCable.createConsumer();
}).call(this);
【问题讨论】:
-
主持人点击开始按钮后,您是否同时创建问题页面和回答按钮?
-
是的,这就是@Laith 的计划
标签: ruby-on-rails coffeescript actioncable