From 1dab96524bd0888b0668c42205772efe84cc9397 Mon Sep 17 00:00:00 2001 From: Nils Dittberner Date: Wed, 16 Nov 2016 16:20:26 +0100 Subject: [PATCH] Iteration F5. Tests with errors. --- app/assets/javascripts/channels/products.coffee | 10 ++++++++++ app/channels/products_channel.rb | 11 +++++++++++ app/controllers/products_controller.rb | 4 ++++ 3 files changed, 25 insertions(+) create mode 100644 app/assets/javascripts/channels/products.coffee create mode 100644 app/channels/products_channel.rb diff --git a/app/assets/javascripts/channels/products.coffee b/app/assets/javascripts/channels/products.coffee new file mode 100644 index 0000000..1b508d2 --- /dev/null +++ b/app/assets/javascripts/channels/products.coffee @@ -0,0 +1,10 @@ +App.products = App.cable.subscriptions.create "ProductsChannel", + connected: -> + # Called when the subscription is ready for use on the server + + disconnected: -> + # Called when the subscription has been terminated by the server + + received: (data) -> + # Called when there's incoming data on the websocket for this channel + $('.store #main').html(data.html) diff --git a/app/channels/products_channel.rb b/app/channels/products_channel.rb new file mode 100644 index 0000000..780b39c --- /dev/null +++ b/app/channels/products_channel.rb @@ -0,0 +1,11 @@ +# Be sure to restart your server when you modify this file. Action Cable runs in a loop that does not support auto reloading. +class ProductsChannel < ApplicationCable::Channel + def subscribed + # stream_from "some_channel" + stream_from "products" + end + + def unsubscribed + # Any cleanup needed when channel is unsubscribed + end +end diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index e746a07..9161b7a 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -44,6 +44,10 @@ class ProductsController < ApplicationController if @product.update(product_params) format.html { redirect_to @product, notice: 'Product was successfully updated.' } format.json { render :show, status: :ok, location: @product } + + @products = Product.all + ActionCable.server.broadcast 'products', + html: render_to_string('store/index', layout: false) else format.html { render :edit } format.json { render json: @product.errors, status: :unprocessable_entity }