small medium large xlarge

Generic-user-small
16 Feb 2016, 20:10
Jason Tumblin (4 posts)

Hey everyone, I have been following the book for a class I’m taking and have run into a snag I can’t seem to solve. Most of my error are from typos or missing something while reading and typing. I have been over this so many times and can’t find a solution. At Iteration 10.3 everything looked good until I finished 11.1 F1: Moving the Cart.

I get a nil error on <&= render @cart &> it says that this value is nil and throws errors in 12 of the products_controller tests. I have followed each step exactly and even used git to go back to chapter 10.1 and started over to make sure I didn’t miss anything. Here is my final code for the 11.1 where I’m stuck.

Any help is greatly appreciated.Thank you.

app/views/layouts/application.html.erb

<body class='<%= controller.controller_name %>'>
  <div id="banner">
    <%= image_tag("logo.png") %>
	<%= @page_title || "Pragmatic Bookshelf" %>
  </div>
  <div id="columns">
    <div id="side">
	  <div id="cart">
	    <%= render @cart %>
	  </div>
	  
	  <ul>
	    <li><a href="http://www....">Home</a></li>
		<li><a href="http://www..../faq">Questions</a></li>
		<li><a href="http://www..../news">News</a></li>
		<li><a href="http://www..../contacts">Contacts</a></li>
	  </ul>
	</div>
	<div id="main">
	  <%= yield %>
	</div>
  </div>
</body>

app/views/carts/_cart.html.erb

<h2>Your Cart</h2>
<table>
  <%= render(cart.line_items) %>
    
  <tr class="total_line">
    <td colspan="2">Total</td>
	<td class="total_cell"><%= number_to_currency(cart.total_price) %></td>
  </tr>
</table>
<%= button_to 'Empty cart', cart, method: :delete,
  data: { confirm: 'Are you sure?' } %>

app/views/line_items/_line_item.html.erb

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

app/views/carts/show.html.erb

<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>

<%= render @cart %>

app/controllers/store_controller.rb

class StoreController < ApplicationController
  include CurrentCart
  before_action :set_cart
  def index
	@products = Product.order(:title)
  end
end

Then the two CSS.SCSS for carts and application I’ll post if needed.

Samr_small_pragsmall
16 Feb 2016, 20:10
Sam Ruby (634 posts)

The stack traceback would be the most helpful thing here.

Meanwhile, I would suggest checking that app/controllers/store_controller.rb has the following code:

  include CurrentCart
  before_action :set_cart
Generic-user-small
16 Feb 2016, 21:27
Jason Tumblin (4 posts)

Sorry, I didn’t even think about traces.

on the 127.0.0.1 :3000 page the side bar comes up but the main page is blank. The 127.0.0.1 :3000/product page brings up the error below.

Test trace

ProductsControllerTest#test_should_get_edit:
ActionView::Template::Error: 'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
    app/views/layouts/application.html.erb:17:in `_app_views_layouts_application_html_erb__662473517__587468298'
    test/controllers/products_controller_test.rb:39:in `block in <class:ProductsControllerTest>'

web page …3000/product

ArgumentError in Products#index
Showing /work/depot/app/views/layouts/application.html.erb where line #17 raised:

'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.
Extracted source (around line #17):
15 <div id="side">
16     <div id="cart">
17 	    <%= render @cart %>
18     </div>
19
20 <ul>

Rails.root: /work/depot

Application Trace | Framework Trace | Full Trace
app/views/layouts/application.html.erb:17:in `_app_views_layouts_application_html_erb___502150971__576255698'
Samr_small_pragsmall
16 Feb 2016, 22:52
Sam Ruby (634 posts)

All this appears to be saying is that @cart is nil. So the next question to explore is: what code is supposed to set @cart? That’s why I suggested you look at app/controllers/store_controller.rb.

Generic-user-small
17 Feb 2016, 00:34
Jason Tumblin (4 posts)

I was thinking store_controller set the cart but wasn’t completely sure. Everything in it checks out though. This is how I have my code…. I’m so stumped, started the chapter over and still the same thing.

app/controllers/store_controller.rb

class StoreController < ApplicationController
  include CurrentCart
  before_action :set_cart
  def index
	@products = Product.order(:title)
  end
end
Samr_small_pragsmall
17 Feb 2016, 13:14
Sam Ruby (634 posts)

If you zip up (or .tar.gz) your depot application and send it to me, I’ll take a look at it.

Generic-user-small
17 Feb 2016, 02:41
Jason Tumblin (4 posts)

Thank you very much. I’ve sent it from a westga.edu account.

Samr_small_pragsmall
17 Feb 2016, 21:40
Sam Ruby (634 posts)

Adding a note for people who might come across this later: this was resolved off-list. Without realizing it, Jason had wandered off into section 11.6 content :-) If you want to run the tests before this chapter is complete, look ahead to that section for guidance.

You must be logged in to comment