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 61122
 
# 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__1587492418220410426_55785240'
    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_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__1587492418220410426_57562380'
    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__1587492418220410426_58002060'
    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:
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__1587492418220410426_62666420'
    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_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__1587492418220410426_63426320'
    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:
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__1587492418220410426_58994100'
    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_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__1587492418220410426_57968020'
    test/controllers/products_controller_test.rb:23:in `block in <class:ProductsControllerTest>'
 
bin/rails test test/controllers/products_controller_test.rb:22
 
.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__1587492418220410426_56545440'
    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_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__1587492418220410426_55456420'
    test/controllers/products_controller_test.rb:38:in `block in <class:ProductsControllerTest>'
 
bin/rails test test/controllers/products_controller_test.rb:37
 
........
 
Finished in 1.091766s, 27.4784 runs/s, 57.7047 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