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>
 
<% @products.each do |product| %>
  <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 %>

Associate image clicks with submit button clicks

edit app/assets/javascripts/store.js.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://jashkenas.github.com/coffee-script/
 
$(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

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

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

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
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.2-p320/gems/rake-10.1.1/lib/rake/rake_test_loader
Started
 
CartTest:
     PASS add duplicate product (0.42s) 
     PASS add unique products (0.01s) 
 
ProductTest:
     PASS image url (0.05s) 
     PASS product attributes must not be empty (0.00s) 
     PASS product is not valid without a unique title (0.00s) 
     PASS product is not valid without a unique title - i18n (0.00s) 
     PASS product price must be positive (0.00s) 
 
Finished in 0.499497 seconds.
 
7 tests, 28 assertions, 0 failures, 0 errors, 0 skips
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
Loaded suite /home/rubys/.rvm/gems/ruby-1.9.2-p320/gems/rake-10.1.1/lib/rake/rake_test_loader
Started
 
CartsControllerTest:
     PASS should create cart (0.27s) 
     PASS should destroy cart (0.05s) 
     PASS should get edit (0.08s) 
    ERROR should get index (0.01s) 
          ActionView::Template::Error: undefined method `line_items' for nil:NilClass
          /home/rubys/git/awdwr/edition4/work-192-31/depot/app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___4438150377379697095_33823980'
 
     PASS should get new (0.01s) 
     PASS should show cart (0.01s) 
     PASS should update cart (0.01s) 
 
LineItemsControllerTest:
     FAIL should create line item (0.04s) 
          Expected response to be a redirect to <http://test.host/carts/980190963> but was a redirect to <http://test.host/>
          /home/rubys/git/rails/actionpack/lib/action_dispatch/testing/assertions/response.rb:67:in `assert_redirected_to'
 
     PASS should destroy line item (0.01s) 
    ERROR should get edit (0.01s) 
          ActionView::Template::Error: undefined method `line_items' for nil:NilClass
          /home/rubys/git/awdwr/edition4/work-192-31/depot/app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___4438150377379697095_33823980'
 
    ERROR should get index (0.01s) 
          ActionView::Template::Error: undefined method `line_items' for nil:NilClass
          /home/rubys/git/awdwr/edition4/work-192-31/depot/app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___4438150377379697095_33823980'
 
    ERROR should get new (0.01s) 
          ActionView::Template::Error: undefined method `line_items' for nil:NilClass
          /home/rubys/git/awdwr/edition4/work-192-31/depot/app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___4438150377379697095_33823980'
 
    ERROR should show line item (0.01s) 
          ActionView::Template::Error: undefined method `line_items' for nil:NilClass
          /home/rubys/git/awdwr/edition4/work-192-31/depot/app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___4438150377379697095_33823980'
 
     PASS should update line item (0.01s) 
 
ProductsControllerTest:
     PASS can't delete product in cart (0.01s) 
     PASS should create product (0.04s) 
     PASS should destroy product (0.00s) 
    ERROR should get edit (0.01s) 
          ActionView::Template::Error: undefined method `line_items' for nil:NilClass
          /home/rubys/git/awdwr/edition4/work-192-31/depot/app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___4438150377379697095_33823980'
 
    ERROR should get index (0.03s) 
          ActionView::Template::Error: undefined method `line_items' for nil:NilClass
          /home/rubys/git/awdwr/edition4/work-192-31/depot/app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___4438150377379697095_33823980'
 
    ERROR should get new (0.01s) 
          ActionView::Template::Error: undefined method `line_items' for nil:NilClass
          /home/rubys/git/awdwr/edition4/work-192-31/depot/app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___4438150377379697095_33823980'
 
    ERROR should show product (0.01s) 
          ActionView::Template::Error: undefined method `line_items' for nil:NilClass
          /home/rubys/git/awdwr/edition4/work-192-31/depot/app/views/layouts/application.html.erb:21:in `_app_views_layouts_application_html_erb___4438150377379697095_33823980'
 
     PASS should update product (0.01s) 
 
StoreControllerTest:
     PASS should get index (0.06s) 
 
Finished in 0.703240 seconds.
 
23 tests, 26 assertions, 1 failures, 9 errors, 0 skips
Errors running test:functionals!

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