2012-05-10 3 views
0

私は単純な言葉でしたい:入力値が変更されたら、私はこの値をdbに入れます。 いくつかのajaxを追加したいと思います。私はこれをしたい: 私は私の入力値(focusout)を変更するとき、私は私の方法は次のようになり、取得-parametrを使用して、いくつかのメソッドを呼び出します。レールのショッピングカートにいくつかのajaxを追加するには?

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

私がやりたいすべてをブロック:html要素ののonblurフィールドを使用します単純な言葉で:入力値が変更されると、私はこの値をdbに入れます。 – byCoder

答えて

1

これを行うために、非表示のリンクは必要ありません。 )私はJavaScriptをインライン化この例では

: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} 

、それは本当に(あなたのapplication.js document.readyに行くべき

$(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

最初のピースはどこに書きますか? – byCoder

+0

もう1つ:私のリンクはそうです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 'しかし、2番目の亜種の亜種がそのようなリンクを生成しています(私はgetを使用する必要があります)http://192.168.1.4:3000/line_items/update_quantity/?id=29&quantity=111&cart=27 – byCoder

+0

あなたが尋ねていることは正確には分かりませんが、「タイプ」を「取得」に変更でき、jqueryはajax GETリクエストを作成します –

関連する問題