NoMethodError in Store#index

Thanks in advance

I want to know how to solve No method errors

Agile Rails 4th ed

Great book

But second time I get this type of error

I would like to know how to troubleshoot and resolve

Please see below thanks once again

NoMethodError in Store#index

Showing C:/rails/shop9/app/views/line_items/_line_item.html.erb where
line #9 raised:

undefined method `title’ for nil:NilClass

Extracted source (around line #9):

6: <% end %>


<%= line_item.quantity %>×
9: <%= line_item.product.title %>
10: <%=
number_to_currency(line_item.total_price) %>

Trace of template inclusion: app/views/carts/_cart.html.erb,

Rails.root: C:/rails/shop9
Application Trace | Framework Trace | Full Trace

_app_views_line_items__line_item_html_erb__441883346_13180080__860564653' app/views/carts/_cart.html.erb:3:in_app_views_carts__cart_html_erb___244752806_13248624_537741326’
app/views/layouts/application.html.erb:37:in block in _app_views_layouts_application_html_erb___681624400_16614672_759801029' app/helpers/application_helper.rb:6:inhidden_div_if’

The error message says to look in this file:


where this is happening:

undefined method `title’ for nil:NilClass

That means that nil is calling the title() method. In ruby, nil is an
object that represents nothing. And something that is nothing cannot
call the title() method. So you should look in the specified file for a
place where title() is being called. But you don’t have to look too
hard because the error message gives you the line number: 9. You are
using a text editor with line numbers to write your programs, right?
The error message even shows you the line:


<%= line_item.product.title %>

Because the error says that you are calling nil.title() in that line,
line_item.product must be equal to nil. You will have to dig further
to figure out why line_item.product is nil.