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 %>

8: <td><%= line_item.quantity %>&times;</td> 9: <td><%= line_item.product.title %></td> 10: <td class="item_price"><%= number_to_currency(line_item.total_price) %></td> 11: </tr>

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

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

app/views/line_items/_line_item.html.erb:9:in `_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:in `hidden_div_if' app/views/layouts/application.html.erb:36:in `_app_views_layouts_application_html_erb___681624400_16614672_759801029'

The error message says to look in this file:

C:/rails/shop9/app/views/line_items/_line_item.html.erb

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:

9: <td><%= line_item.product.title %></td>

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