正如标题所说,当我点击按钮时,我发出POST or DELETE
请求,然后我呈现新的更改。再次按下该按钮时,仅渲染,但控制台输出表示渲染是第一次进行的。
jQuery的,
$('#qty-n-price-row-<%= @index %>').html("<%= escape_javascript
render(:partial => 'shared/line_items/qty_n_price_row',
:locals => { index: @index, item: @item } ) %>");
修改 更多细节
每行按钮,bootstrap:
<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
shared / static_page / dep_and_cat_list partial:
<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 :(得分:0)
我发现了问题,当我减少/增加物品的数量时,我没有更新我发送给部分的参数
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 } ) %>");