small medium large xlarge

26 Apr 2012, 05:03
Michael Glaz (4 posts)

I’m trying to run the code in app/views/carts/show.html.erb on page 112/113. But I’m getting this error: undefined method `title’ for nil:NilClass

help, mike

02 May 2012, 08:14
oppih Xue (1 post)

I’ve also got this problem, I wonder if someone can point out where should we can reference to get rid of this error

04 May 2012, 02:20
Scott Herrington (6 posts)

I am also experiencing this issue.

undefined method title’ for nil:NilClass`

5: <h2>Your Pragmatic Cart</h2>
6: <ul>    
7:   <% @cart.line_items.each do |item| %>
8:     <li><%= item.product.title %></li>
9:   <% end %>
10: </ul>

Up to this point all has worked well. I had to make the changes suggested in the errata for the MassAssignmentSecurity::Error.

(product_id: product_id)

All of my associations appear to be in order.

I also inspected the database and build method appears to be working.

The log files yield a 500 error.

I am using Rails 3.2.3 on Mac OS X 10..7.3.

Any help would be greatly appreciated.

03 Jul 2013, 21:40
richard thomas (1 post)


This is probably too late to help any of the above, but I had the same problem yesterday and eventually figured out what was causing the problem for my set-up at least.

The error message is arising because ‘item’ is nil or empty. For me, this was occurring as I’d somehow managed to generate entries in the LineItems table which had a valid cart_id but nil product_id. This could have happened when I had some errors earlier in the development…

Anyway, the problem was resolved by simply erasing the contents of the LineItems table. At the command prompt in your work directory call up a Rails console:

$ rails console

Then type:


The table remains but the contents are erased. Any nil vales should now be gone and hopefully the error will not appear.

04 Oct 2013, 19:39
John Blaze (1 post)

Richard, my rails console didn’t recognize LineItems, so that didn’t work for me. What did work was rake db:reset