0

As the title says, when I click a button I issue a POST or DELETE request, then I render that new changes. Render only kicks in when pressing that button again, but console output says that render was made on first time too.

jQuery,

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

EDIT More details

Buttons in each row, 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>

Line item controller

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>

this is based on 35104257

Community
  • 1
  • 1
maris ancans
  • 55
  • 1
  • 11

1 Answers1

0

I found the problem, when I decreased / increased quantity for item, I didnt update params that I sent to partial

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

And then sent new params

render(:partial => 'shared/line_items/qty_n_price_row', :locals => { index: @index, item: @item } ) %>");
maris ancans
  • 55
  • 1
  • 11