Rails 5动作电缆与nobrainer不能正常工作

时间:2016-01-11 18:59:17

标签: ruby-on-rails rethinkdb rails-activejob actioncable

我尝试了来自https://github.com/rails/actioncable-examples的动作电缆示例。它与ActiveRecord配合得很好。当使用NoBrainer进行rethinkDB时,我得到 ActiveJob :: SerializationError(不支持的参数类型:注释)。 所以我只是通过id而不是自己像下面的activejob。但是有线电视服务器无法监听任何频道,并且不会调用CommentsChannel方法。

comment.rb

after_save { CommentRelayJob.perform_later(self.id) }

*****comment_relay_job.rb*****
def perform(comment_id)
  comment = Comment.find(comment_id)
  ActionCable.server.broadcast "messages:#{comment.message_id}:comments",
  comment: CommentsController.render(partial: 'comments/comment', locals: { comment: comment }
end

comments_channel.rb

module ApplicationCable
  class Channel < ActionCable::Channel::Base; end
end

class CommentsChannel < ApplicationCable::Channel
  def follow(data)
    stop_all_streams
    stream_from "messages:#{data['message_id'].to_i}:comments"
end

  def unfollow
    stop_all_streams
  end
end

comments.coffee

App.comments = App.cable.subscriptions.create "CommentsChannel",    collection: -> $("[data-channel='comments']")
  connected: ->
    setTimeout =>
       @followCurrentMessage()
       @installPageChangeCallback()
    , 1000

  received: (data) ->
    @collection().append(data.comment) unless @userIsCurrentUser(data.comment)

  userIsCurrentUser: (comment) ->
       $(comment).attr('data-user-id') is $('meta[name=current-user]').attr('id')

  followCurrentMessage: ->
    if messageId = @collection().data('message-id')
      @perform 'follow', message_id: messageId
    else
      @perform 'unfollow'

  installPageChangeCallback: ->
     unless @installedPageChangeCallback
       @installedPageChangeCallback = true
       $(document).on 'page:change', -> App.comments.followCurrentMessage()

0 个答案:

没有答案