javascript - Ruby on Rails updating list with AJAX breaks order -
i have list of elements in order. problem when update element ajax element jumps on bottom of list. when reload page returns on right place though. i'd prefer element stay in place when updating.
the list:
<div class="shopping-cart"> <%= render "shopping_cart" %> </div>
_shopping_cart.html.erb:
<% @order_items.each |order_item| %> <%= render 'carts/cart_row', order_item: order_item %> <% end %>
_cart_row.html.erb:
<%= form_for order_item, remote: true |f| %> <%= f.number_field :quantity, class: "form-control cart-quantity" %> <%= f.submit "submit", class: "btn" %> <% end %>
update.js.erb:
$(".shopping-cart").html("<%= escape_javascript(render 'carts/shopping_cart') %>")
any ideas why happens? thank you.
update
the controller code:
@order_items = current_order.order_items.order("created_at asc")
and there no more ajax code, here, there no other sort criteria.
for lack of answer, i'll give idea...
--
firstly, must remember ajax calls, issue either going ajax's appending, or rails' data.
i presumed issue you're appending new data view. you've done code have; issue @order_items
array.
here's i'd do:
update.js.erb: $(".shopping-cart").html("<%=j render 'carts/shopping_cart' %>") _shopping_cart.html.erb: <% render 'carts/cart_row', collection: @order_items, as: :order_item %>
the controller code:
@order_items = current_order.order_items.order :created_at
no difference?
sure.
the way debug this:
- check
@order_items
object (eithernetwork
tab in browser dev console), or in rails logger - check js isn't doing stupid data. if data ordered properly, should append required.
Comments
Post a Comment