简单的数量功能问题,购物车

时间:2010-01-17 21:59:55

标签: ruby-on-rails ruby shopping-cart agile

我刚刚使用Rails 3rd Ed。选择了Agile Web Development,我将通过Depot Application章节,我正在尝试创建一个简单的Edit数量函数和删除函数。我已经幸运了删除功能,但编辑数量功能没有运气。

我将提供大量信息,所以请不要感到不知所措。我发现这是一个具有挑战性的问题。

添加到add_to_cart.html.erb

<div class="cart-title">Your cart</div>

<table>
<% for item in @cart.items %>
<tr>

<td><% form_for @cart.items, :url => {:action => "cart_update", :id => "#{item.getinventoryid}"} do |f| %>
  <%= f.text_field :quantity, :size => '3' %>
  <%= f.hidden_field :id, :value => "#{item.getinventoryid}" %>
  <%= f.submit 'cart_update' %>
<% end %></td>

<td><%=h item.quantity %> &times;</td>
<td><%=h item.title %></li></td>
<td><%=h item.description %></td>
<td class="item-price"><%= number_to_currency(item.price) %></td>
<td><%= link_to 'remove', {:controller => 'inventories', :action => 'remove_cart_item', :id => "#{item.getinventoryid}"} %></td>


</tr>
<% end %>

<tr class="total-line">
<td colspan="4">Total</td>
<td class="total-cell"><%= number_to_currency(@cart.total_price) %></td>
</tr>
</table>

<%= button_to "Checkout", :action => 'checkout' %>
<%= button_to 'Empty cart', :action => 'empty_cart' %>

inventories_controller:

def cart_update
        @inventory = Inventory.find(params[:id])
        @cart = find_cart
        @cart.increment_inventory_quantity(params[:inventory])
    end

    def remove_cart_item
        inventory = Inventory.find(params[:id])
        @cart = find_cart
        @cart.remove_inventory(inventory)
        redirect_to_index("The item was removed")
    end

Cart.rb模型

attr_accessor :items

def increment_inventory_quantity(id, quantity)
   inventory_to_increment = @items.select{|item| item.inventory == inventory}

   # We do this because select will return an array
   unless product_to_increment.empty?
      inventory_to_increment = inventory_to_increment.first
   else
      # error handling here
   end

   inventory_to_increment.quantity = quantity
end

def remove_inventory(inventory)
   @items.delete_if {|item| item.inventory == inventory }
end

cart_item.rb模型

attr_accessor :inventory, :quantity

def getinventoryid
        @inventory.id
    end

这会产生奇怪的结果: alt text

请注意,我的循环(#Fail)中的两个项目中都显示数量 16 。当我在InventoriesController中提交表单时出现ArgumentError# cart_update 错误的参数数量(1表示2)将返回错误。传递的参数:

{"commit"=>"cart_update",
 "_method"=>"put",
 "authenticity_token"=>"sH1tWXTJPltpSq5XaAkww7259IR5ZiflnqSFB2Zb0IY=",
 "id"=>"50",
 "cart_item"=>{"quantity"=>"16",
 "id"=>"50"}} 

2 个答案:

答案 0 :(得分:1)

你确定它是form_for(@ cart.items)而不是form_for(item)吗?

答案 1 :(得分:1)

您获取错误的参数数量错误,因为您在控制器方法中将一个参数传递给@cart.increment_inventory_quantity。该方法需要两个参数。

# In your controller:
def cart_update
  @inventory = Inventory.find(params[:id])
  @cart = find_cart
  @cart.increment_inventory_quantity(params[:inventory]) # you are passing one thing
end
# Then in your model:
def increment_inventory_quantity(id, quantity) # the method wants two things
  # ...

可能你打算做这样的事情:

def cart_update
  @inventory = Inventory.find(params[:cart_item][:id])
  @cart = find_cart
  @cart.increment_inventory_quantity(@inventory.id, params[:cart_item][:quantity])
end