small medium large xlarge

25 Apr 2013, 22:56
Phil Moss (5 posts)

It seems like the blind effect on my hidden_div helper is not working. When the cart is empty, it won’t appear when adding an item to the cart. After I refresh the page the new items that I posted (that didn’t show up before) are magically there, then the ajax works perfectly when I add more items. I have triple checked all of my code against the book, also copied and pasted from the examples and it still doesn’t work.

This is what I get in the log… no errors though.

Started POST "/line_items?product_id=2" for at 2013-04-25 16:45:10 -0600
Processing by LineItemsController#create as JS
  Parameters: {"authenticity_token"=>"8Xxu4A8oRhiM/4PNPC98lOhw18/2AF9j5ye3UfhWMfo=", "product_id"=>"2"}
  Cart Load (0.0ms)  SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1  [["id", 19]]
  Product Load (0.0ms)  SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1  [["id", "2"]]
  LineItem Load (0.0ms)  SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = ? AND "line_items"."product_id" = 2 LIMIT 1  [["cart_id", 19]]
   (0.0ms)  begin transaction
  SQL (1.0ms)  UPDATE "line_items" SET "quantity" = ?, "updated_at" = ? WHERE "line_items"."id" = 57  [["quantity", 4], ["updated_at", Thu, 25 Apr 2013 22:45:10 UTC +00:00]]
   (28.4ms)  commit transaction
  LineItem Exists (0.0ms)  SELECT 1 AS one FROM "line_items" WHERE "line_items"."cart_id" = ? LIMIT 1  [["cart_id", 19]]
  LineItem Load (0.0ms)  SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = ?  [["cart_id", 19]]
  Product Load (0.0ms)  SELECT "products".* FROM "products" WHERE "products"."id" = ? ORDER BY "products"."id" ASC LIMIT 1  [["id", 4]]
  Product Load (0.0ms)  SELECT "products".* FROM "products" WHERE "products"."id" = ? ORDER BY "products"."id" ASC LIMIT 1  [["id", 3]]
  Product Load (0.0ms)  SELECT "products".* FROM "products" WHERE "products"."id" = ? ORDER BY "products"."id" ASC LIMIT 1  [["id", 2]]
  Rendered line_items/_line_item.html.erb (6.0ms)
  Rendered carts/_cart.html.erb (13.3ms)
  Rendered line_items/create.js.erb (16.6ms)
Completed 200 OK in 60ms (Views: 22.1ms | ActiveRecord: 29.4ms)

26 Apr 2013, 14:16
Sam Ruby (634 posts)

You are not giving us much to go on :-)

I’d suggest doing a view source on the storefront page. Verify that the following is present (spacing doesn’t matter):

<div id="cart" style="display: none">
  <h2>Your Cart</h2>
    <tr class="total_line">
      <td colspan="2">Total</td>
      <td class="total_cell">$0.00</td>

Also check for:

<script data-turbolinks-track="true" src="/assets/jquery-ui.js?body=1"></script>
26 Apr 2013, 15:47
Phil Moss (5 posts)

I apologize for the vagueness.

I am missing the <h2>Your Cart</h2> until I “buy” a couple items and refresh the page. Then after I inspect the element it shows up, but obviously without the display: none.

Here is what my _cart partial template looks like:

<% unless cart.line_items.empty? %>
Your Cart
<%= render(cart.line_items) %> Total <%= number_to_currency(cart.total_price) %> <%= button_to 'Empty cart', cart, method: :delete, data: { confirm: 'Are you sure?' } %> <% end %> `
26 Apr 2013, 16:06
Phil Moss (5 posts)

And yes, jquery-ui is showing up in my source.

26 Apr 2013, 17:16
Phil Moss (5 posts)


<!DOCTYPE html>

  Pragprog Books Online Store
  <%= stylesheet_link_tag    "application", media: "all",
    "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>

<%= image_tag("logo.png") %> <%= @page_title || "Pragmatic Bookshelf" %>
<%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %> <%= render @cart %> <% end %>
<%= yield %>
` And my application_helper.rb:
module ApplicationHelper
  def hidden_div_if(condition, attributes = {}, &block)
    if condition
      attributes["style"] = "display: none"
    content_tag("div", attributes, &block)

26 Apr 2013, 18:38
Sam Ruby (634 posts)

Ah… the fix is pretty easy. Drop the unless cart.line_items.empty? line and the corresponding end line.

That code (at the top of 11.4) was meant to show one, somewhat brutal, way to accomplish this, and is followed up with So, let’s not use this code. Simply delete those two lines, and see if things work a bit better.

26 Apr 2013, 19:34
Phil Moss (5 posts)

Thanks, Sam!

Apparently I need to learn to read.

27 Apr 2013, 02:18
Sam Ruby (634 posts)

Don’t be so hard on yourself :-)

I remember when I was slogging through the first edition as a reader myself. Many mistakes, and I learned a lot in the process.

You must be logged in to comment