AJAX using rails: the first item is not updated on the cart

I just started learning Ruby on Rails using Agile Web Development by Sam Ruby. I'm stuck in task F now: adding Ajax to the cart. Everything went well until I added code to hide the empty cart. Now, when I add the first item, the cart is displayed on the sidebar (it should be displayed with one item in the cart), but when I add the second item, the cart shows with two items, as it should. The code works if I add more items. I ran into a problem only when adding the first item to the cart. I tore off my hair during the day on this issue. Any help would be appreciated. I apologize if I did not provide full information. Please let me know additional data, if any, that will be relevant. I use rails 3.2.8 and ruby ​​1.9.3 Thank you!

_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 %> &times;</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> 

~

+4
source share
1 answer

I managed to solve it myself :) .. when I typed the problem, I got a hint that the problem was somehow with the rendering itself, and therefore it was ... the solution is to set the show parameter to 0 in {$ ( '#cart'). show ('blind', 1000); } Now the code should be {$ ('# cart'). show ('blind', 0); }

@Btuman done !!

+4
source

Source: https://habr.com/ru/post/1432035/


All Articles