0

簡単な言葉でやりたいこと:入力値が変更されたら、この値をdbに入れます。ajaxを追加したいと思います。これを実行したい:入力値(focusout)を変更すると、get-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

ご覧のとおり、入力フォーカスが外れると、新しい非表示のリンクを取得し、それを呼び出します。これは、上部に記述されているメソッドを呼び出します。私は何をする必要がありますか?作成するファイルと場所、およびこれを行う方法は?いくつかのajaxが欲しい)

簡単な言葉でやりたいこと:入力値が変更されたら、この値をdbに入れます。

4

1 に答える 1

1

これを達成するために隠しリンクは必要ありません。html 要素の onblur フィールドを使用します。

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

この例では、javascript をインライン化しました。実際には、application.js の document.read() ブロックに入れる必要があります。

$(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')} 
        })
  })
})
于 2012-05-10T23:35:29.033 に答える