Skip to content

Instantly share code, notes, and snippets.

@pavel-sazonov
Last active June 27, 2018 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavel-sazonov/40fc77b8a815ce5944b0c3ee8f6f86d8 to your computer and use it in GitHub Desktop.
Save pavel-sazonov/40fc77b8a815ce5944b0c3ee8f6f86d8 to your computer and use it in GitHub Desktop.
#application.js
var App = App || {}
App.cable = ActionCable.createConsumer();
#questions.coffee
ready = ->
questionsList = $('.questions-list')
.......
App.cable.subscriptions.create('QuestionsChannel', {
connected: ->
@perform 'follow'
,
received: (data) ->
questionsList.append data
})
$(document).ready(ready)
$(document).on('turbolinks:load', ready)
#questions_channel.rb
class QuestionsChannel < ApplicationCable::Channel
def follow
stream_from 'questions'
end
end
#questions_controller.rb
.....
after_action :publish_question, only: [:create]
private
def publish_question
return if @question.errors.any?
ActionCable.server.broadcast(
'questions',
ApplicationController.render(
partial: 'questions/question_for_index',
locals: { question: @question }
)
)
end
....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment