I have just started learning Ruby on Rails using Agile Web Develop ment by Sam Ruby. I'm currently stuck on task F: adding Ajax to cart. Everything went well till the time I added to the code for hiding an empty cart. Now when I add the first item the cart shows up empty on the side bar (it should show up with one item n the cart) but when i add the second item the cart shows up with 2 items, as it should.The code works if I add more items. I'm facing the problem only when adding the first item on the cart. I've been tearing my hair out for a day now on this problem. Any help will be greatly appreciated. Apologies if I haven't furnished complete details. Please let me know the additional details, if any, that would be relevant. I'm using rails 3.2.8 and ruby 1.9.3 Thanks!
_cart.html.erb
<div class="cart_title">Your Cart</div>
<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, confirm: 'Are you sure?'%>
_line_item.html.erb
<%if @current_item==line_item%>
<tr id="current_item">
<% else %>
<tr>
<% end %>
<td><%= line_item.quantity %> ×</td>
<td><%= line_item.product.title %></td>
<td class="item_price" ><%= number_to_currency(line_item.total_price) %></td>
<td><%= button_to 'Remove Item', line_item, method: :delete, confirm: 'Are you sure?'%>
</tr>
create.js.erb
$("#notice").hide();
if ($('#cart tr').length > 0) { $('#cart').show('blind', 1000); }
$('#cart').html("<%=j render @cart %>");
$('#current_item').css({'background-color':'#88cc88'}).
animate({'background-color':'#114411'}, 1000);
application.js.erb
<html>
<head>
<title>Pragprog Books Online Store</title>
<!-- START:stylesheet -->
<%= stylesheet_link_tag "scaffold" %>
<%= stylesheet_link_tag "depot", :media => "all" %><!-- <label id="code.slt"/> -->
<!-- END:stylesheet -->
<%= javascript_include_tag "application" %>
<%= csrf_meta_tag %><!-- <label id="code.csrf"/> -->
</head>
<body id="store">
<div id="banner">
<%= image_tag("logo.png") %>
<%= @page_title || "Pragmatic Bookshelf" %><!-- <label id="code.depot.e.title"/> -->
</div>
<div id="columns">
<div id="side">
<a href="/">Home</a><br />
<a href="/faq">Questions</a><br />
<a href="/news">News</a><br />
<a href="/contact">Contact</a><br />
<%if @cart%>
<%= hidden_div_if(@cart.line_items.empty?, id:"cart") do%>
<%=render @cart%>
<% end %>
<% end %>
</div>
<div id="main">
<%= yield %><!-- <label id="code.depot.e.include"/> -->
</div>
</div>
</body>
</html>
~
managed to solve it myself :)..while typing out the problem i got a hint that the problem was somehow with the rendering itself and so it was .. the solution is to set the show parameter to 0 in { $('#cart').show('blind', 1000); } the code should now be { $('#cart').show('blind', 0); }
@Btuman done!!