2009-06-12 70 views
1

我有一个使用会话(会话存储在数据库中)的虚拟购物车。 我使用AJAX调用将产品添加到购物车。这个列表也是可排序的。 我遇到的问题是更新购物车中产品的分拣位置,因为购物车是虚拟的。任何人都可以帮助(特别是使用sortable_element:update操作)。下面是一些代码:没有数据库的可排序列表

#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 

回答