I have a virtual shopping cart using sessions (sessions stored in the DB). I add products to the cart using an AJAX call. This list is also sortable. The problem I am having is updating the sort position of the products in the cart, given that the shopping cart is virtual. Can anyone help (specifically with the sortable_element :update action). Here is some of the code:
#cart.rb
class Cart
attr_reader :items
def initialize
@items = []
end
def add_product(product
@items << CartItem.new(product)
end
end
#cart_item.rb
class CartItem
attr_reader :product
def initialize(product)
@product = product
end
def name
@product.name
end
end
#cart/index.html.erb
<div id="items">
<%= render :partial => 'cart', :object => @cart %>
</div>
#cart/_cart.html.erb
<%= render :partial => 'cart_item', :collection => @cart.items %>
<%= sortable_element "items", :url => {:action => :update} %>
#cart/_cart_item.html.erb
<% content_tag_for :li, cart_item do %>
<p><%= cart_item.name %></p>
<% end %>
#cart_controller.rb
def index
find_cart
end
def update
#???? how does I change the sort of @cart?
render :nothing => true
end
def find_cart
session[:cart] ||= @cart
end