Agile Web Development with Rails, Edition 5

11.6 Iteration F6: Testing AJAX changes 11.4 Iteration F4: Hide an Empty Cart

11.5 Iteration F5: Broadcasting Updates

create a channel

edit app/channels/application_cable/products_channel.rb
class ProductsChannel < ActionCable::Channel::Base
  def subscribed
    stream_from "products"
  end
end

send updates when price changes

edit app/controllers/products_controller.rb
  def update
    respond_to do |format|
      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 }
      end
    end
  end

Update price when notified of price changes

edit app/assets/javascripts/products.coffee
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
 
App.productsChannel = 
  App.cable.subscriptions.create { channel: "ProductsChannel" },
    received: (data) -> $(".store #main").html(data.html)

Run tests... oops.

rails test
Run options: --seed 58129
 
# Running:
 
...E
 
Error:
CartsControllerTest#test_should_get_index:
ActionView::Template::Error: undefined method `line_items' for nil:NilClass
    app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___3869942147280966853_45932180'
    test/controllers/carts_controller_test.rb:9:in `block in <class:CartsControllerTest>'
 
bin/rails test test/controllers/carts_controller_test.rb:8
 
...E
 
Error:
LineItemsControllerTest#test_should_show_line_item:
ActionView::Template::Error: undefined method `line_items' for nil:NilClass
    app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___3869942147280966853_63506840'
    test/controllers/line_items_controller_test.rb:38:in `block in <class:LineItemsControllerTest>'
 
bin/rails test test/controllers/line_items_controller_test.rb:37
 
E
 
Error:
LineItemsControllerTest#test_should_get_edit:
ActionView::Template::Error: undefined method `line_items' for nil:NilClass
    app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___3869942147280966853_65096200'
    test/controllers/line_items_controller_test.rb:43:in `block in <class:LineItemsControllerTest>'
 
bin/rails test test/controllers/line_items_controller_test.rb:42
 
...E
 
Error:
LineItemsControllerTest#test_should_get_index:
ActionView::Template::Error: undefined method `line_items' for nil:NilClass
    app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___3869942147280966853_68242080'
    test/controllers/line_items_controller_test.rb:9:in `block in <class:LineItemsControllerTest>'
 
bin/rails test test/controllers/line_items_controller_test.rb:8
 
E
 
Error:
LineItemsControllerTest#test_should_get_new:
ActionView::Template::Error: undefined method `line_items' for nil:NilClass
    app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___3869942147280966853_68479980'
    test/controllers/line_items_controller_test.rb:14:in `block in <class:LineItemsControllerTest>'
 
bin/rails test test/controllers/line_items_controller_test.rb:13
 
E
 
Error:
ProductsControllerTest#test_should_show_product:
ActionView::Template::Error: undefined method `line_items' for nil:NilClass
    app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___3869942147280966853_68769920'
    test/controllers/products_controller_test.rb:38:in `block in <class:ProductsControllerTest>'
 
bin/rails test test/controllers/products_controller_test.rb:37
 
E
 
Error:
ProductsControllerTest#test_should_get_index:
ActionView::Template::Error: undefined method `line_items' for nil:NilClass
    app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___3869942147280966853_68911900'
    test/controllers/products_controller_test.rb:18:in `block in <class:ProductsControllerTest>'
 
bin/rails test test/controllers/products_controller_test.rb:17
 
E
 
Error:
ProductsControllerTest#test_should_get_edit:
ActionView::Template::Error: undefined method `line_items' for nil:NilClass
    app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___3869942147280966853_69347940'
    test/controllers/products_controller_test.rb:43:in `block in <class:ProductsControllerTest>'
 
bin/rails test test/controllers/products_controller_test.rb:42
 
....E
 
Error:
ProductsControllerTest#test_should_get_new:
ActionView::Template::Error: undefined method `line_items' for nil:NilClass
    app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___3869942147280966853_63510900'
    test/controllers/products_controller_test.rb:23:in `block in <class:ProductsControllerTest>'
 
bin/rails test test/controllers/products_controller_test.rb:22
 
........
 
Finished in 0.892276s, 33.6219 runs/s, 70.6059 assertions/s.
 
30 runs, 63 assertions, 0 failures, 9 errors, 0 skips

11.6 Iteration F6: Testing AJAX changes 11.4 Iteration F4: Hide an Empty Cart