Agile Web Development with Rails, Edition 4

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

11.5 Iteration F5: Making Images Clickable

Review our current storefront markup

edit app/views/store/index.html.erb
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
 
<h1>Your Pragmatic Catalog</h1>
 
<% cache ['store', Product.latest] do %>
  <% @products.each do |product| %>
    <% cache ['entry', product] do %>
      <div class="entry">
        <%= image_tag(product.image_url) %>
        <h3><%= product.title %></h3>
        <%= sanitize(product.description) %>
        <div class="price_line">
          <span class="price"><%= number_to_currency(product.price) %></span>
          <%= button_to 'Add to Cart', line_items_path(product_id: product),
            remote: true %>
        </div>
      </div>
    <% end %>
  <% end %>
<% end %>

Associate image clicks with submit button clicks

edit app/assets/javascripts/store.js.coffee
 
 
$(document).on "ready page:change", ->
  $('.store .entry > img').click ->
    $(this).parent().find(':submit').click()

The page looks no different

get /

Your Cart

CoffeeScript $36.00
Total $36.00

Your Pragmatic Catalog

Cs f56ef62bc41b040664e801a38f068082a75d506d9048307e8096737463503d0b

CoffeeScript

CoffeeScript is JavaScript done right. It provides all of JavaScript's functionality wrapped in a cleaner, more succinct syntax. In the first book on this exciting new language, CoffeeScript guru Trevor Burnham shows you how to hold onto all the power and flexibility of JavaScript while writing clearer, cleaner, and safer code.

$36.00
Ruby 836fee16e3757bcd4e2c21d064eee4fa81d7f93e4e9a44b1ffcd6c1d41c92a88

Programming Ruby 1.9 & 2.0

Ruby is the fastest growing and most exciting dynamic language out there. If you need to get working programs delivered fast, you should add Ruby to your toolbox.

$49.95
Rtp d3d2240713cce773840785de837acf822d235d1b77040a3e1ed9d8c014cff086

Rails Test Prescriptions

Rails Test Prescriptions is a comprehensive guide to testing Rails applications, covering Test-Driven Development from both a theoretical perspective (why to test) and from a practical perspective (how to test effectively). It covers the core Rails testing tools and procedures for Rails 2 and Rails 3, and introduces popular add-ons, including Cucumber, Shoulda, Machinist, Mocha, and Rcov.

$34.95

Run tests... oops.

rake test
Run options: --seed 29093
 
# Running:
 
EE...EE..EE.F.EE......E.......
 
Finished in 0.519149s, 57.7868 runs/s, 121.3524 assertions/s.
 
  1) 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___3730201457595560794_42579980'
    test/controllers/products_controller_test.rb:24:in `block in <class:ProductsControllerTest>'
 
 
  2) 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___3730201457595560794_42579980'
    test/controllers/products_controller_test.rb:39:in `block in <class:ProductsControllerTest>'
 
 
  3) 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___3730201457595560794_42579980'
    test/controllers/products_controller_test.rb:18:in `block in <class:ProductsControllerTest>'
 
 
  4) 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___3730201457595560794_42579980'
    test/controllers/products_controller_test.rb:44:in `block in <class:ProductsControllerTest>'
 
 
  5) 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___3730201457595560794_42579980'
    test/controllers/line_items_controller_test.rb:9:in `block in <class:LineItemsControllerTest>'
 
 
  6) 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___3730201457595560794_42579980'
    test/controllers/line_items_controller_test.rb:34:in `block in <class:LineItemsControllerTest>'
 
 
  7) Failure:
LineItemsControllerTest#test_should_create_line_item [/home/rubys/git/awdwr/edition4/work-42/depot/test/controllers/line_items_controller_test.rb:28]:
Expected response to be a redirect to <http://test.host/carts/980190963> but was a redirect to <http://test.host/>.
Expected "http://test.host/carts/980190963" to be === "http://test.host/".
 
 
  8) 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___3730201457595560794_42579980'
    test/controllers/line_items_controller_test.rb:39:in `block in <class:LineItemsControllerTest>'
 
 
  9) 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___3730201457595560794_42579980'
    test/controllers/line_items_controller_test.rb:15:in `block in <class:LineItemsControllerTest>'
 
 
 10) 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___3730201457595560794_42579980'
    test/controllers/carts_controller_test.rb:9:in `block in <class:CartsControllerTest>'
 
30 runs, 63 assertions, 1 failures, 9 errors, 0 skips

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