2017-06-02 63 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 –

相關問題