我正在Rails 5.2应用程序中设置Action电缆,以在列表页面上发表评论。 jQuery不是我的强项,但我敢肯定我的目标是正确的。加载页面时收到此错误。我已经禁用了Turbolinks,所以我确定对jQuery coffee脚本的调用是正确的。我已经仔细检查了文件的位置和错别字,并且我一生都无法弄清楚为什么未调用方法“ 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
结束
app / assets / javascripts / application.js
//= require jquery
//= require rails-ujs
//= require popper
//= require bootstrap-sprockets
//= require snackbar
//= require activestorage
//= require cable
//= require_tree .
不知道我还能在这里添加什么。很难找到与该错误相关的任何内容,但是很明显,在此过程中并没有发现任何内容。
任何帮助将不胜感激。谢谢大家
答案 0 :(得分:2)
注意:我尚未亲自使用ActionCable,因此请多加些盐。
在app/channels/listings_channel.rb
文件中,ApplicationCable::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