2013-09-17 3 views
2

아약스를 사용하여 새 값으로 테이블을 업데이트하고 싶습니다. 아이디어는 무엇입니까? 나는 사용자가 제품의 수량을 변경할 수있는 카트를 가지고 있습니다. 나는 전체 페이지를 새로 고침하고 싶지 않다. 테이블을 새로 고침하고 싶습니다. 현재 전체 페이지를 다시로드하는 것으로 작동합니다.레일즈 4 아약스 업데이트 div

내가 한 것.

수량 값 입력 양식이 있습니다. 카트 (ManyToMany)와 제품을 연결하기 위해 나는 line_item을 사용합니다. 그래서 모든 line_item은 장바구니에있는 제품을 나타냅니다.

<div id="cart_table"> 
    <%= render @cart %> 
</div> 

다음은 업데이트 양식 @cart

<table class="table table-hover table-condensed"> 
    <tbody> 
    <%= render cart.line_items %> 
    </tbody> 
</table> 

Line_items 렌더링 카트의이보기 : line_items의

<%= form_tag(line_item_path, method: "put", remote: true, class: "table_form") do %> 
    <%= hidden_field_tag("product_id", value = line_item.product.id) %> 
    <label> 
    <%= number_field_tag "quantity", value = line_item.quantity, in: 1...11 %> 
    <%= submit_tag "update", class: "btn btn-mini" %> 
    </label> 
<% end %> 

컨트롤러 :

def update 
    @cart = current_cart 
    @line_item = @cart.update_product(params[:product_id], params[:quantity]) 
    respond_to do |format| 
     if @line_item.save 
      format.html { redirect_to current_cart, notice: 'Changed' } 
      format.js 
     else 
      format.html { render action: "new" } 
     end 
    end 
end 

여기 내가 붙어 있습니다. 내 update.js.rjs에 무엇을 넣어야합니까? 지금은이 라인을

$('#cart_table').replaceWith("<%= escape_javascript(render @cart) %>"); 

을하지만 아무 일도 발생하지 않습니다 내가 콘솔에서 오류가 발생합니다 :

send 
jQuery.extend.ajax 
$.rails.rails.ajax 
$.rails.rails.handleRemote 
(anonymous function) 
jQuery.event.dispatch 
elemData.handle 

이 작은 문제를 해결하기 위해 몇 가지 아이디어를 얻을 것이 좋을 것이다.

감사

편집 : JS 파일에

$('#cart_table').html("<%= escape_javascript(render @cart) %>"); 

를 사용하는 것이 좋습니다.

답변

5

update.js.erb 대신 update.js.rjs

+0

예이 트릭을 수행을 사용해보십시오. 정말 고마워! –