Rails 5.1 - ActionCable没有更新?

问题描述 投票:0回答:1

当我尝试使用ActionCable发布评论时,一切正常,但是,我总是必须刷新页面才能看到评论本身被渲染。任何想法都错了吗?试图重新启动服务器,注销,登录等。但没有什么真正帮助。

blogs.coffee

jQuery(document).on 'turbolinks:load', ->
  comments = $('#comments')
  if comments.length > 0
    App.global_chat = App.cable.subscriptions.create {
      channel: "BlogsChannel"
      blog_id: comments.data('blog-id')
    },
    connected: ->
    disconnected: ->
    recieved: (data) ->
      comments.append data['comment']
    send_comment: (comment, blog_id) ->
      @perform 'send_comment', comment: comment, blog_id: blog_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('blog-id')
      textarea.val('')
    e.preventDefault()
    return false

Blogs_channel.rb

class BlogsChannel < ApplicationCable::Channel
  def subscribed
    stream_from "blogs_#{params['blog_id']}_channel"
  end

  def unsubscribed
  end

  def send_comment(data)
    current_user.comments.create!(content: data['comment'], blog_id: data['blog_id'])
  end
end

Comment.rb

class Comment < ApplicationRecord
  belongs_to :user
  belongs_to :blog

  validates :content, presence: true, length: { minimum: 1, maximimum: 1000 }

  after_create_commit { CommentBroadcastJob.perform_later(self) }
end

Komnnbardquistjb.rb

class CommentBroadcastJob < ApplicationJob
  queue_as :default

  def perform(comment)
    ActionCable.server.broadcast "blogs_#{comment.blog.id}_channel", comment: render_comment(comment)
  end


  private

  def render_comment(comment)
    CommentsController.render partial: 'comments/comment', locals: { comment: comment }
  end
end

_comment.html.erb

<div class="comment-card">

  <div class="card">
    <div class="card-block">
      <div class="row">
        <div class="col-md-1">

        </div>

        <div class="col-md-11">
          <%= comment.content %>
        </div>
      </div>
    </div>
  </div>
</div>

注释将在show.html.erb页面内呈现:

<%= render 'comments/comment_form' %>



<div id="comments" data-blog-id="<%= @blog.id %>">
  <%= render @blog.comments %>
</div>

提交新评论时的控制台日志:

[ActionCable] [[email protected]] [1] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2017-12-21 09:42:47 +0100
[ActionCable] [[email protected]] [1] BlogsChannel stopped streaming from blogs_3_channel
Started GET "/cable" for 127.0.0.1 at 2017-12-21 09:42:48 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2017-12-21 09:42:48 +0100
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ?  [["id", 1], ["LIMIT", 1]]
[ActionCable] [[email protected]] [1] Registered connection (Z2lkOi8vdWRlbXktdHV0b3JpYWwtcmFpbHMvVXNlci8x)
[ActionCable] [[email protected]] [1] BlogsChannel is transmitting the subscription confirmation
[ActionCable] [[email protected]] [1] BlogsChannel is streaming from blogs_3_channel

Chrome控制台内部没有任何错误或任何错误。

有任何想法吗?在route.rb里面我使用mount ActionCable.server => '/cable',如果这可能有帮助。它似乎没有被激活/使用,不知道为什么。

提前致谢!

编辑:

在某些时候,我在终端日志中收到以下错误:

[ActiveJob] [CommentBroadcastJob] [b32437ee-cf29-4323-97e9-841c87300518] Error performing CommentBroadcastJob (Job ID: b32437ee-cf29-4323-97e9-841c87300518) from Async(default) in 82.51ms: ActionView::Template::Error (undefined local variable or method `comment' for #<#<Class:0x00007f9b948bbed8>:0x00007f9b93a8fb98>
Did you mean?  ��comment):

在def执行中执行byebug,结果:

[2, 11] in 
/app/jobs/comment_broadcast_job.rb
    2: class CommentBroadcastJob < ApplicationJob
    3:   queue_as :default
    4: 
    5:   def perform(comment)
    6:     byebug
=>  7:     ActionCable.server.broadcast "blogs_#{comment.blog.id}_channel", comment: render_comment(comment)
    8:   end

好吧,为什么评论未定义?再次,如果我刷新页面,即使出现此错误,我也会看到评论被渲染。

ruby-on-rails websocket coffeescript ruby-on-rails-5 actioncable
1个回答
0
投票

所以我从原始文件(它来自一个课程)中复制了BroadCastJob.rb文件,并且它最终正常工作。我在文件中找不到任何拼写错误与教师文件。如果有人能找到它,请告诉我。

我的CommentBroadcastJob.rb文件:

class CommentBroadcastJob < ApplicationJob
  queue_as :default

  def perform(comment)
    ActionCable.server.broadcast "blogs_#{comment.blog.id}_channel", comment: render_comment(comment)
  end


  private

  def render_comment(comment)
    CommentsController.render partial: 'comments/comment', locals: { comment: comment }
  end
end

教官一:

class CommentBroadcastJob < ApplicationJob
  queue_as :default

  def perform(comment)
    ActionCable.server.broadcast "blogs_#{comment.blog.id}_channel", comment: render_comment(comment)
  end

  private

  def render_comment(comment)
    CommentsController.render partial: 'comments/comment', locals: { comment: comment }
  end
end

这是有效的。每当我删除这个并使用我的那个,我得到与以前相同的错误。当我使用教师的那些时,它最终在发送后呈现消息。不知道差异应该在哪里。

© www.soinside.com 2019 - 2024. All rights reserved.