0

我正在嘗試在GoRails.com上的actioncable指南之後創建一個ActionCable聊天室。NoMethodError - 未定義的方法`safe_constantize'爲零:NilClass

由於我試圖連接到ActionCable我獲得以下錯誤: NoMethodError - 未定義的方法`safe_constantize」的零:NilClass

[ActionCable] [User 1] Could not execute command from 
{"command"=>"subscribe", "identifier"=>" 
{\"ChatroomsChannel\":\"ChatroomsChannel\",\"room_id\":1}"}) 
[NoMethodError - undefined method `safe_constantize' for 
nil:NilClass]: /Users/Mathias/.rvm/gems/ruby- 
[email protected]/gems/actioncable- 
5.0.0.rc2/lib/action_cable/connection/subscriptions.rb:29:in `add' | 
/Users/Mathias/.rvm/gems/[email protected]/gems/actioncable- 
5.0.0.rc2/lib/action_cable/connection/subscriptions.rb:15:in 
`execute_command' | /Users/Mathias/.rvm/gems/ruby- 
[email protected]/gems/actioncable- 
5.0.0.rc2/lib/action_cable/connection/base.rb:88:in 
`dispatch_websocket_message' | /Users/Mathias/.rvm/gems/ruby- 
[email protected]/gems/actioncable- 
5.0.0.rc2/lib/action_cable/server/worker.rb:54:in `block in invoke' | 
/Users/Mathias/.rvm/gems/[email protected]/gems/actioncable- 
5.0.0.rc2/lib/action_cable/server/worker.rb:39:in `block in work' 

我的應用程序/渠道/ chatrooms_channel.rb看起來是這樣的:

def subscribed 
    @chatroom = Chatroom.find(params[:room_id]) 
    #puts params[:room_id] 
    #current_user.join_room(@chatroom) 
    stream_from "chatrooms:#{@chatroom.id}" 
    # stream_from "some_channel" 
end 

也許值得一提的是,我升級到從軌道4

軌5.0.0.rc2謝謝

+0

不熟悉action_cable,但它似乎期望'channel'作爲頻道名稱的關鍵字,而不是'ChatroomsChannel'。 –

回答

0
assets/javascripts/channels/chatroom.coffee

如果您需要分配params,頻道名稱必須在PARAMS哈希太:

App['chatroom' + roomId] = App.cable.subscriptions.create { 
    channel: 'ChatroomChannel', 
    room_id: roomId 
}, { 
    received: -> 
    ... 
} 

且僅當你不需要params,你可以寫沒有哈希頻道名稱:

App.chatroom = App.cable.subscriptions.create 'ChatroomChannel', 
    received: -> 
    ... 
相關問題