2012-09-03 5 views
4

Sam Ruby의 Agile Web Develop ment를 사용하여 Ruby on Rails를 배우기 시작했습니다. 저는 현재 작업 F에 붙어 있습니다 : Ajax를 카트에 추가하십시오. 빈 카트를 숨기기위한 코드를 추가 할 때까지 모든 것이 잘 진행되었습니다. 이제 첫 번째 항목을 추가하면 카트가 사이드 바에 비어있는 것으로 나타납니다 (카트에서 한 항목 만 표시). 두 번째 항목을 추가하면 카트가 2 개 항목으로 표시됩니다. 코드가 작동합니다. 항목을 더 추가하면됩니다. 장바구니에 첫 번째 항목을 추가 할 때만 문제가 발생합니다. 나는이 문제에 대해 하루 하루 내 머리카락을 찢어 버렸다. 어떤 도움이라도 대단히 감사하겠습니다. 완전한 세부 사항을 제공하지 않았다면 죄송합니다. 관련성이있는 추가 세부 정보가있는 경우 알려 주시기 바랍니다. 레일 3.2.8과 루비 1.9.3을 사용하고 있습니다. 고마워요!레일을 사용하는 AJAX : 첫 번째 항목이 카트에서 업데이트되지 않습니다.

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

~

+0

나는 스스로를 해결할 수 있었다. .. 문제를 타이핑하는 동안 문제는 어떻게 든 렌더링 자체와 함께 있었고 그래서 .. 해결책은 show 매개 변수를 {$ ('#cart'). show ('blind', 1000); } 코드는 이제 {$ ('cart')이어야합니다. show ('blind', 0); } –

+0

해결 한 경우 답변을 게시하고 받아들입니다. :) – Btuman

답변

4

직접 해결할 수 있습니다. 문제를 타이핑하는 동안 문제는 어떻게 든 렌더링 자체와 관련이 있으므로 해결책은 show 매개 변수를 다음과 같이 설정하는 것입니다. {$ ('# cart')에 0 점. show ('blind', 1000); } 코드는 이제 {$ ('cart')이어야합니다. show ('blind', 0); }

@Btuman 완료!