簡體   English   中英

AJAX使用rails:第一項沒有在購物車上更新

[英]AJAX using rails: first item does not get updated on cart

我剛剛開始使用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, 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>

設法解決它自己:) ..在輸入問題的時候我得到了一個提示,問題是以某種方式呈現本身,所以它是...解決方案是在{$('#cart中將show參數設置為0 ')。show('blind',1000); 代碼現在應該是{$('#cart')。show('blind',0); }

@Btuman完成!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM