2017-06-02 45 views
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%>时,前置动作不再工作了。

回答

2

您似乎在频道文件中存在拼写错误。渲染局部时,必须使用locals,而不是local。所以,做了以下变化:

html = ApplicationController.render(partial: 'groups/group', local: { 
    group: group 
}) 

变化locallocals

html = ApplicationController.render(partial: 'groups/group', locals: { 
    group: group 
}) 
+0

酷!非常感谢你@Laith Azer,你正在拯救我的夜晚! –

+0

非常欢迎:) –

+0

好抓@LaithAzer –

相关问题