2016-04-05 14 views
0

タイトルには、POST or DELETEリクエストを発行するボタンをクリックすると、その新しい変更がレンダリングされます。 そのボタンをもう一度押すとレンダリングが行われますが、がコンソールに出力され、レンダリングが初めて行われたことが示されます。Ajaxの最初のリクエストは、Railsを使用してレンダリングに失敗します

のjQuery、

$('#qty-n-price-row-<%= @index %>').html("<%= escape_javascript 
    render(:partial => 'shared/line_items/qty_n_price_row', 
    :locals => { index: @index, item: @item }) %>"); 

EDIT 詳細各行の

ボタン、ストラップ:

<div class="col-xs-3 col-md-3 col-md-3" id="qty-n-price-row-<%= index + 1 %>"> 
         <%= render 'shared/line_items/qty_n_price_row', item: item, index: index + 1%> 
         </div> 
         <div class="col-xs-1 col-md-1 col-md-1"> 
          <%= button_to qty_line_item_path(item), method: :post, remote: true, params: { index: index + 1 } do%> 
           <span class="glyphicon glyphicon-plus"></span> 
          <% end %> 
          <%= button_to qty_line_item_path(item), method: :delete, remote: true, params: { index: index + 1 } do%> 
           <span class="glyphicon glyphicon-minus"></span> 
          <% end %> 
         </div> 

申込コントローラ

def qty 

@item = @cart.line_items.find(params[:id]) 
@index = params[:index] 
if request.post? #-> increment 
    increase 
elsif request.delete? #-> decrement 
    decrease 
end 

#@item.send(method, :qty, params[:qty]) 
end 

def decrease 
@line_item = @cart.decrease(params[:id]) 
respond_to do |format| 
    @index = params[:index] 
    if @line_item.save 
    format.html { redirect_to store_path, notice: 'Item was successfully updated.' } 
    format.js { @current_item = @line_item } 
    format.json { head :ok } 
    else 
    format.html { render action: "edit" } 
    format.json { render json: @line_item.errors, status: :unprocessable_entity} 
    end 
end 
end 

def increase 
@line_item = @cart.increase(params[:id]) 
@index = params[:index] 
respond_to do |format| 
    if @line_item.save 
    format.html { redirect_to store_path, notice: 'Item was successfully updated.' } 
    format.js { @current_item = @line_item } 
    format.json { head :ok } 
    else 
    format.html { render action: "edit" } 
    format.json { render json: @line_item.errors, status: :unprocessable_entity } 
    end 
end 
end 

Cart.rb

def decrease(line_item_id) 
current_item = line_items.find(line_item_id) 
if current_item.quantity > 1 
    current_item.quantity -= 1 
else 
    current_item.destroy 
end 
current_item 
end 

def increase(line_item_id) 
current_item = line_items.find(line_item_id) 
current_item.quantity += 1 
current_item.save 
current_item 
end 

共有/ static_page/dep_and_cat_list部分:

<div class="col-xs-4 col-md-4 col-md-4 text-right"> 
<h6><strong><span class="text-muted"> €<%= item.product.price %>      
<span> x </span></strong></h6> 
</div> 
<div class="col-xs-4 col-md-4 col-md-4"> 
    <input type="text" class="form-control input-sm" value="<%=  item.quantity %>"> 
</div> 
<div class="col-xs-4 col-md-4 col-md-4"> 
<h6><strong> <span> = <%= item.total_price %></span></strong></h6> 

これは35104257

+0

新しいのparamsを送っ答えるために詳細が必要なように思えます。 –

+0

@marisancansはブラウザのデバッグコンソールを開いて、どちらの場合もサーバーから要求への応答が有効であることを確認します。それは、異なる場合は、サーバーの部分に見て、コードのクライアント部分を検証するはい。 –

+0

これを見ていただきありがとうございます、問題はサーバー側です、私は部分的にそれらを送信する前に、更新パラメータをdidnt –

答えて

0

に基づいているのI /増加量を減少させたときに、私は、問題を発見しましたアイテムのために、私は部分に送られた更新パラメータを捨てました

def increase 
@line_item = @cart.increase(params[:id]) 
respond_to do |format| 
    if @line_item.save 
    @item = @cart.line_items.find(params[:id])<-- Added ths 

そしては

render(:partial => 'shared/line_items/qty_n_price_row', :locals => { index: @index, item: @item }) %>"); 
関連する問題