微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AJAX使用rails:第一项没有在购物车上更新

我刚刚开始使用Sam Ruby的Agile Web Develop学习Ruby on Rails.我目前停留在任务F:将Ajax添加到购物车.一切顺利,直到我添加到隐藏空车的代码.现在当我添加一个项目时,购物车在侧栏上显示为空(它应该显示在购物车中有一个项目)但是当我添加第二个项目时,购物车显示有2个项目,因为它应该.代码工作如果我添加更多项目.只有在购物车上添加第一件商品时才会遇到问题.在这个问题上,我已经把头发撕了一天了.任何帮助将不胜感激.如果我没有提供完整的细节,请道歉.请告知我相关的其他详细信息(如果有的话).我正在使用rails 3.2.8和ruby 1.9.3谢谢!

_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,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>

解决方法

设法解决它自己:) ..在输入问题的时候我得到了一个提示,问题是以某种方式呈现本身,所以它是…解决方案是在{$(‘#cart中将show参数设置为0 ‘).show(‘blind’,1000);代码现在应该是{$(‘#cart’).show(‘blind’,0); }

@Btuman完成!!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐