small medium large xlarge

23 Nov 2011, 09:48
Peter Mellett (1 post)

Hi All,

I’ve run the tests at the end of the AJAX section and resolved all but one of the errors (as directed in the book) although I have one that isn’t accounted for but I don’t quite know how to fix.

I get the following on @rake test@

  1) Error:
ActionView::Template::Error: undefined method `line_items' for #<Array:0x48970f8>
    n:/projects/depot/app/views/layouts/application.html.erb:22:in `_app_views_layouts_application_html_erb___635173282_35781168'

This is caused because the test case for the Cart Controller initialises the @cart variable, which is then interpreted by the application template as being valid, so it attempts to display the cart code. Because the cart is invalid it then presents the error.

I’m not sure how to account for this in either the logic of displaying the cart at all, or the test case (obviously, I get an error page if I browse to http://localhost:3000/carts so this is undesirable.

I hope that makes sense, Peter

edit: Here’s my @carts.yml@ (automatically generated)

# Read about fixtures at

# This model initially had no columns defined.  If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
one: {}
# column: value
two: {}
#  column: value

and here is the relevant code for the cart display from @app\views\layouts\application.html.erb@

	  <% if @cart %>
        <%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %>
          <%= render @cart %>
        <% end %>
	  <% end %>
25 Nov 2011, 01:52
Sam Ruby (633 posts)

This suggests that @cart is an array, not a single cart. I would suggest taking a careful look at the index method in app/controllers/carts_controller.rb. Are you by any chance setting @cart (singular) instead of @carts (plural) in that method?

You must be logged in to comment