Rails 5.2 中的 Action Cable 抛出 NoMethodError

Action Cable in Rails 5.2 throwing a NoMethodError

我正在我的 Rails 5.2 应用程序中设置 Action 电缆,以便在列表页面上发表评论。 jQuery 不是我的强项,但我确信我的目标是正确的。我在加载页面时收到此错误。我已经禁用了 Turbolinks,所以我确定对 jQuery 咖啡脚本的调用是正确的。我已经仔细检查了文件位置和拼写错误,但终究无法弄清楚为什么 'channel' 方法没有被调用。这是文件。

app/assets/javascripts/channels/listings.咖啡

jQuery ->
comments = $('#comments')
if comments.length > 0 
    App.global_chat = App.cable.subscriptions.create {
        channel: "ListingsChannel"
        listing_id: comments.data("listing-id")
    },
    connected: ->
    disconnected: ->
    received: (data) ->
        comments.append data['comment']
    send_comment: (comment, listing_id) ->
        @perform 'send_comment', comment: comment, listing_id: listing_id
$('#new_comment').submit (e) ->
    $this = $(this)
    textarea = $this.find('#comment_content')
    if $.trim(textarea.val()).length > 1
        App.global_chat.send_comment textarea.val(),
        comments.data('listing-id')
        textarea.val('')
    e.preventDefault()
    return false

app/channels/listings_channel.rb

class ListingsChannel < ApplicationCable::channel
    def subscribed
        # point to the stream
        stream_from "listings_#{params['listing_id']}_channel"
    end

    def unsubscribed
    end

    # uses the method in the coffee script to get the data required to attach to the comment
    def send_comment(data)
        current_user.comments.create!(content: data['comment'], listing_id: data['listing_id'])
    end
end

错误信息:

[ActionCable] [bradley@email.com] [2] Registered connection (Z2lkOi8vZ2xvYmFsLWFieC9Vc2VyLzI)
[ActionCable] [bradley@email.com] [2] Could not execute command from ({"command"=>"subscribe", "identifier"=>"{\"channel\":\"ListingsChannel\",\"listing_id\":6}"}) [NoMethodError - undefined method `channel' for ApplicationCable:Module]: /Users/bradley/Development/app_name/app/channels/listings_channel.rb:1:in

app/channels/application_cable/connection.rb

module ApplicationCable
      class Connection < ActionCable::Connection::Base
            identified_by :current_user

            def connect
                self.current_user = find_verified_user
                logger.add_tags 'ActionCable', current_user.email
                logger.add_tags 'ActionCable', current_user.id
            end

            protected

            def find_verified_user
                # recreating devise functionality for current_user methods
                if verified_user = env['warden'].user
                    verified_user
                end
            end
      end
 end

app/controllers/comments_controller.rb

class CommentsController < ApplicationController
def create
    # grabs current user and builds the comment based on relationships set up
    @comment = current_user.comments.build(comment_params)
end

private

def comment_params
    params.require(:comment).permit(:content)
end

结束

app/jobs/comment_broadcast_job.rb

class CommentBroadcastJob < ApplicationJob
#create queue/list served in order
queue_as :default

def perform(comment)
    # start broadcast on actioncable / create a channell/ render comment
    ActionCable.server.broadcast "listings_#{comment.listing.id}_channel", comment: render_comment(comment)
end

private

def render_comment(comment)
    # call the comments controller and render the partial in the views, pass in the variable to render
    CommentsController.render partial: 'comments/comment', locals: { comment: comment }
end

结束

app/assets/javascripts/application.js

//= require jquery
//= require rails-ujs
//= require popper
//= require bootstrap-sprockets
//= require snackbar
//= require activestorage
//= require cable
//= require_tree .

检查表单是否有任何错误的 # 调用或任何东西。

不确定我还能在这里添加什么。很难找到与该错误相关的任何内容,但很明显它没有找到任何东西。

如有任何帮助,我们将不胜感激。谢谢大家。

Note: I have yet to use ActionCable myself so take my response with a grain of salt.

在您的 app/channels/listings_channel.rb 文件中 ApplicationCable::channel 应该在频道上有大写字母 "C"。

当前: class ListingsChannel < ApplicationCable::channel

要求: class ListingsChannel < ApplicationCable::Channel

看起来 Ruby 你的 ApplicationCable 模块中有一个名为 channel 的函数,这就是你收到错误的原因。

此外,由于您没有 post 代码,请确保您在 ApplicationCable 命名空间中有一个 Channel class。基于 this repository,您的应用程序文件夹中还应包含此文件:

# /channels/application_cable/channel.rb
module ApplicationCable
  class Channel < ActionCable::Channel::Base
  end
end