0
我目前正在学习ActionCable这是太棒了(而不是错误)。 而我正面临一个奇怪的问题。行动电缆正在工作,而不是我推动数据
我的目标是为团队创建建立一个渠道。允许用户在不刷新的情况下查看新的。
在这一点上,一切都很好。但是当我尝试显示我的组的名称时,ActionCable不再工作。组创建得很好,但它们不能实时显示。所以我必须刷新页面才能看到它们。任何人都可以帮我解决这个问题吗?
我的代码:
控制器(基团):
def index
@company = current_user.company
@groups = @company.groups
end
信道(基团):
class GroupsChannel < ApplicationCable::Channel
def subscribed
stream_from "groups"
end
def speak(data)
group = Group.create(name: data['group'], company_id: 12)
html = ApplicationController.render(partial: 'groups/group', local: {
group: group
})
ActionCable.server.broadcast 'groups', group: html
end
end
咖啡文件(组):
App.groups = App.cable.subscriptions.create "GroupsChannel",
connected: ->
$(document).on 'keypress', '#group_name', (event) =>
if (event.keyCode == 13)
@speak(event.target.value)
$('#group_name').val('')
$('#MyNewGroup').modal('toggle')
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
$('#groups_area').append(data.group)
speak: (group) ->
@perform 'speak', {group: group}
视图(指数):
<div class="container">
<h1 style="text-align:center; margin-bottom: 30px; margin-top: 10px;">Vos groupes</h1>
<div id="groups_area">
<%= render @groups%>
</div>
</div>
视图(_group)
<div class="col-md-3">
<div class="panel panel-success">
<div class="panel-heading">
<h3 style="margin-top:0; text-align:center">
====================================Problem========================================
<%= group.name %>
====================================Problem========================================
</h3>
</div>
<div class="panel-body">
test body
</div>
</div>
</div>
所以,当我添加此行<%= group.name%>时,前置动作不再工作了。
酷!非常感谢你@Laith Azer,你正在拯救我的夜晚! –
非常欢迎:) –
好抓@LaithAzer –