ruby-on-rails - Rails 5.2 中的 Action Cable 抛出 NoMethodError

标签 ruby-on-rails ruby redis actioncable

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

app/assets/javascripts/channels/listings.coffee

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

结束

应用程序/ Assets /javascripts/application.js

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

检查表单以查看是否有任何错误的 # 调用或其他任何内容。 enter image description here

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

任何帮助将不胜感激。谢谢大家。

最佳答案

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

在你的app/channels/listings_channel.rb文件 ApplicationCable::channel channel 上应该有一个大写的“C”。

电流: class ListingsChannel < ApplicationCable::channel

必需: class ListingsChannel < ApplicationCable::Channel

看起来像 Ruby 的东西有一个函数叫做 channel在你的ApplicationCable模块,这就是您收到错误的原因。

此外,由于您没有发布代码,请确保您有一个 Channel ApplicationCable 中的类命名空间。基于 this repository ,你的应用程序文件夹中也应该有这个文件:

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

关于ruby-on-rails - Rails 5.2 中的 Action Cable 抛出 NoMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52275761/

相关文章:

ruby-on-rails - Rails 3 ActiveModel 嵌套类 I18n

ruby - 如何在 Volt 中创建 jQuery 切换类?

ruby-on-rails - 具有关系的表单中的自动填充 ID

Redis 在几个 hsets 中递增几个字段

redis - 如何清除redis中的SLOWLOG?

ruby-on-rails - 如何在ransack上设置带有字段名称的默认条件?

ruby-on-rails - force_ssl 在 Rails 中有什么作用?

ruby-on-rails - 如何综合分析我的 Ruby 测试套件?

ruby - 看到 Ruby 的内幕了吗?

PHP Redis超时,连接读取错误?