使用rails的AJAX:购物车上的第一个项目不会得到更新
我刚刚开始使用SamRuby的敏捷Web开发学习RubyonRails。我目前正忙于任务F:将Ajax添加到购物车。一切都很顺利,直到我添加了隐藏空购物车的代码。现在,当我添加第一个项目时,购物车在侧栏上显示为空(它应该显示为购物车中的一个项目),但当我添加第二个项目时,购物车显示为2个项目,这是应该的。如果我添加更多项目,代码会起作用。只有在将第一个项目添加到购物车时,我才会遇到这个问题。为了这个问题,我已经忙了一天了。任何帮助都将不胜感激。抱歉,如果我没有提供完整的细节。请让我知道相关的其他细节(如有)。我正在使用rails 3.2.8和ruby 1.9.3,谢谢 _cart.html.erb使用rails的AJAX:购物车上的第一个项目不会得到更新,ajax,ruby-on-rails-3,depot,Ajax,Ruby On Rails 3,Depot,我刚刚开始使用SamRuby的敏捷Web开发学习RubyonRails。我目前正忙于任务F:将Ajax添加到购物车。一切都很顺利,直到我添加了隐藏空购物车的代码。现在,当我添加第一个项目时,购物车在侧栏上显示为空(它应该显示为购物车中的一个项目),但当我添加第二个项目时,购物车显示为2个项目,这是应该的。如果我添加更多项目,代码会起作用。只有在将第一个项目添加到购物车时,我才会遇到这个问题。为了这个问题,我已经忙了一天了。任何帮助都将不胜感激。抱歉,如果我没有提供完整的细节。请让我知道相关的其他
<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?'%>
您的购物车
全部的
_行_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);
$(“#注意”).hide();
如果($('cart tr').length>0){$('cart').show('blind',1000);}
$('#cart').html(“”);
$('current_item').css({'background-color':'88cc88'})。
制作动画({'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>
普拉格图书在线商店
“全部”%>
~我自己设法解决了:)…在键入问题时,我得到了一个提示,问题不知怎么地与渲染本身有关,因此。。解决方案是在{$('#cart').show('blind',1000);}中将show参数设置为0。代码现在应该是{$('#cart').show('blind',0);}
@B人类完成了 我自己设法解决了:)…在输入问题的时候,我得到了一个提示,问题出在渲染本身,所以。。解决方案是在{$('#cart')中将show参数设置为0。show('blind',1000);}代码现在应该是{$('#cart')。show('blind',0);}如果您解决了它,则将其发布到答案中并接受它:)