gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:04:57 24 4
gpt4 key购买 nike

我正在我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com