Agile Web Development with Rails, Edition 4

Agile Web Development with Rails, Edition 4

12.1 Iteration G1: Capturing an Order 11.4 Iteration F4: Hide an Empty Cart

11.6 Iteration F6: Testing AJAX changes

Verify that yes, indeed, the product index is broken.

get /products

NoMethodError in Products#index

Showing /home/rubys/git/awdwr/edition4/work-30/depot/app/views/layouts/application.html.erb where line #20 raised:

undefined method `line_items' for nil:NilClass

Extracted source (around line #20):

17:     <div id="side">
18: <!-- START_HIGHLIGHT -->
19:       <!-- START:hidden_div -->
20:       <%= hidden_div_if(@cart.line_items.empty?, :id => 'cart') do %>
21:         <%= render @cart %>
22:       <% end %>
23:     <!-- END:hidden_div -->

Rails.root: /home/rubys/git/awdwr/edition4/work-30/depot

Application Trace | Framework Trace | Full Trace
app/views/layouts/application.html.erb:20:in `_app_views_layouts_application_html_erb__935027895_70064183935800_0'
app/controllers/products_controller.rb:7:in `index'

Request

Parameters:

None

Show session dump

Show env dump

Response

Headers:

None

Conditionally display the cart.

edit app/views/layouts/application.html.erb
      <% if @cart %>
        <%= hidden_div_if(@cart.line_items.empty?, :id => 'cart') do %>
          <%= render @cart %>
        <% end %>
      <% end %>

Update the redirect test.

edit test/functional/line_items_controller_test.rb
  test "should create line_item" do
    assert_difference('LineItem.count') do
      post :create, :product_id => products(:ruby).id
    end
 
    assert_redirected_to store_path
  end

Add an XHR test.

edit test/functional/line_items_controller_test.rb
  test "should create line_item via ajax" do
    assert_difference('LineItem.count') do
      xhr :post, :create, :product_id => products(:ruby).id
    end 
 
    assert_response :success
    assert_select_rjs :replace_html, 'cart' do
      assert_select 'tr#current_item td', /Programming Ruby 1.9/
    end
  end

Add an test in support for the coffeescript changes.

edit test/functional/store_controller_test.rb
  test "markup needed for store.js.coffee is in place" do
    get :index
    assert_select '.store .entry > img', 3
    assert_select '.entry input[type=submit]', 3
  end

Run the tests again.

rake test
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rake-0.9.2.2/lib/rake/rake_test_loader
Started
........
Finished in 0.418402 seconds.
 
8 tests, 29 assertions, 0 failures, 0 errors
Loaded suite /home/rubys/.rvm/gems/ruby-1.8.7-p352/gems/rake-0.9.2.2/lib/rake/rake_test_loader
Started
.........................
Finished in 1.078228 seconds.
 
25 tests, 43 assertions, 0 failures, 0 errors

Save our progress

git commit -a -m "AJAX"
[master 16462d6] AJAX
 3 files changed, 42 insertions(+), 11 deletions(-)
git tag iteration-f

12.1 Iteration G1: Capturing an Order 11.4 Iteration F4: Hide an Empty Cart