2010-06-18 45 views
0

嘿傢伙們,我一直在試圖理解文檔並找到一個例子,但我很茫然。Form With Quantity似乎沒有提交

這只是購物車內的更新數量提交表單。但是,更新的數量沒有保存到數據庫 - 它總是使數量爲0.請幫助。

<% for line_item in @cart.line_items %> 
    <% form_for :lineitems, :url => {:controller => "line_items", :action => "cart_update", :id => "#{line_item.product_id}"} do |l| %> 
    <%= l.text_field :quantity, :size => '3', :value => line_item.quantity %> 
    <%= l.submit 'cart_update' %> 
<% end %> 

路線

map.connect 'line_item_update', :controller => 'line_items', :action => 'cart_update' 

控制器

def cart_update 
    @product = Product.find(params[:id]) 
    item = LineItem.find_or_create_by_cart_id(:cart_id => current_cart.id, :product_id =>  @product.id, :quantity => 0, :unit_price => @product.price) 
    item.quantity = (params[:quantity]).to_i 
    item.save 
    redirect_to :controller => 'products' 
end 

回答

0

對於需要改變V開始查看這樣的事情:

<% form_for :lineitems, :url => {:controller => "line_items", :action => "cart_update" do |l| %> 
    <% @cart.line_items.each_with_index do |line_item, index| %> 
    <% fields_for "@cart.line_items[#{index}]", line_item do |i| %> 
     <%= i.text_field :quantity, :size => '3', :value => line_item.quantity %> 
    <% end %> 
    <% end %> 
<%= l.submit 'cart_update' %> 
+0

此論壇帖子可能會對您有所幫助:http://railsforum.com/viewtopic.php?id=2696 – thomasfedb 2010-06-18 15:49:15