2012-05-10 3 views
0

간단히 말해서 : 입력 값이 변경되면이 값을 db로 지정합니다. 일부 아약스를 추가하고 싶습니다. 나는이 작업을 수행 할 : 내 입력 값 (대한 focusOut)를 변경하면 , 난 어떤 메소드를 호출 얻을-parametr를 사용하여, 내 방법은 다음과 같습니다 : 그것은 어쩌면이장바구니에 레일에 아약스를 추가하는 방법은 무엇입니까?

def update_quantity 
    @cart = current_cart 
    @line_item = LineItem.find(params[:id]) 
    respond_to do |format| 
     if @line_item.update_attribute(:quantity, params[:quantity]) #&& @cart.id == params[:cart] 
     format.html { redirect_to(@line_item, :notice => 'Line item was successfully updated.') } 
     format.xml { head :ok } 
     else 
     format.html { render :action => "edit" } 
     format.xml { render :xml => @line_item.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 

이것은 테스트 작업, 그래서 나쁜 솔루션, 내보기는 다음과 같습니다 : 보시다시피

%p 
    = line_item.art_name 
    = line_item.ART_ID 
    = line_item.art_code 
    &times 
    .cart-quantity 
     = line_item.quantity   
     %input{ :class => "quantity", :value => line_item.quantity } 
     = link_to "обновить", :controller => "line_items", :action => "update_quantity", :id => line_item.id, :quantity => line_item.quantity, :cart => @cart.id  
    = line_item.total_price 

, 내가 원하는 : 입력 포커스 아웃 때, 나는 새로운 숨겨진 링크를 얻고, 호출, 메소드를 호출 상위 기록 된 어떤 . 내가해야 할 일은 무엇입니까? 어떤 파일을 만들고, 어디에서, 어떻게해야합니까? 일부 아약스를 원한다.)

나는 단순한 단어로하고 싶다 : 입력 값이 변경되면이 값을 db로 바꾼다.

+0

내가 원하는 모든 간단한 단어로 : 입력 값이 변경되면이 값을 db로 설정합니다. – byCoder

답변

1

이렇게하려면 숨겨진 링크가 필요하지 않습니다. html 요소의에 onblur 필드 사용 차단, 정말) (당신의 application.js의 document.ready에 가야 내가 자바 스크립트를 인라인이 예에서는

:javascript 
    function updateQuantity() { 
    $.ajax({ 
     url: "/line_items/update_quantity", 
     type: "POST", 
     data: {id: $(this).attr('id'), quantity: $(this).attr('quantity'), cart: $(this).attr('cart_id')} 
    }) 
    } 

%input.quantity{:value => line_item.quantity, :onblur => updateQuantity(); :id => line_item.id, :quantity => line_item.quantity, :cart => @cart.id} 

$(document).ready(function() { 
    $(".quantity").onfocusout(function() { 
    $.ajax({ 
      url: "/line_items/update_quantity", 
      type: "POST", 
      data: {id: $(this).attr('id'), quantity: $(this).attr('quantity'), cart: $(this).attr('cart_id')} 
     }) 
    }) 
}) 
+0

첫 번째 peice를 쓸 위치는? – byCoder

+0

한 번 더 : my link is http://192.168.1.4:3000/line_items/update_quantity/id=28&quantity=2&cart=27 그런 다음 결과가 나타납니다. 내 경로는 'line_items/: action/id = : id & quantity = : quantity & cart = : cart '=>'line_items # update_quantity '하지만 두 번째 변형 아약스가 그런 링크를 생성하고 있습니다 (get을 사용해야합니다) http://192.168.1.4:3000/line_items/update_quantity/?id=29&quantity=111&cart=27 – byCoder

+0

정확히 무엇을 요구하고 있는지 확실하지 않지만 '유형'을 '가져 오기'로 변경할 수 있으며 jquery는 아약스 GET 요청을합니다 –

관련 문제