【问题标题】:edit content in ruby on rails shopping cart在 ruby​​ on rails 购物车中编辑内容
【发布时间】:2010-11-27 16:51:45
【问题描述】:

我正在尝试使用导轨构建一个简单的购物车,现在我可以将产品添加到购物车,我想知道如何在购物车中编辑产品,我正在使用会话来控制购物中的产品大车。这是用户添加到购物车时看到的内容:

<% @cart.items.each do |item| %>
<tr>
    <td>
        <%= image_tag item.pic , :alt => "#{item.title}" %>
    </td>
    <td>
        <%= link_to "#{item.title}" , store_path(item.product_id) %>
    </td>
    <td>
        <%= item.unit_price %>
    </td>
    <td>
        <%= item.quantity %>
    </td>
    <td>
        <%= item.total_price %>
    </td>
    <% end %>
</tr>

这是 CartItem 类:

class CartItem

  attr_reader :product, :quantity

  def initialize(product)
    @product = product
    @quantity = 1
  end

  def increment_quantity
    @quantity += 1
  end

  def product_id
    @product.id
  end

  def title
    @product.name
  end

  def pic
    @pic = @product.photo.url(:thumb)
  end

  def unit_price
    @product.price
  end

  def total_price
    @product.price * @quantity
  end

end

我想让用户能够编辑产品数量或删除产品,而不仅仅是清除整个购物车。我该怎么做?

【问题讨论】:

    标签: ruby-on-rails shopping-cart


    【解决方案1】:

    嗯,你已经以一种接近的方式进行了一些设置。您的购物车项目模型中有 increment_quantity 方法,因此您需要设置购物车模型以允许您指定产品,然后调用新方法,如下所示:

    cart.rb(假设这是您的购物车型号)

    def increment_product_quantity(id, quantity)
       product_to_increment = @items.select{|product| product.product_id == id}
    
       # We do this because select will return an array
       unless product_to_increment.empty?
          product_to_increment = product_to_increment.first
       else
          # your error handling here
       end
    
       product_to_increment.quantity = quantity
    end
    
    def remove_product(id)
       @items.delete_if {|product| product.product_id == id }
    end
    

    现在,您必须将您的购物车项目模型修改为数量不是 attr_reader 对象,而是一个 attr_accessor 对象,或者专门为您设置数量的购物车项目创建一个方法;你的选择。

    还有一些其他的事情可以做,但这大致是我现在可以推荐的最简单和最干净的方法。

    【讨论】:

    • 我是否需要添加新的控制器操作(即 Edit_Cart)?还是在我的 add_to_cart 操作中添加编辑更有意义?
    【解决方案2】:

    好问题。我能够使删除功能正常工作。看来您正在关注实用程序员Agile Web Development with Rails,第三版书籍。

    我们开始...

    添加到 add_to_cart.html.erb

    我在最后一个 tr 行项目旁边添加了下表行:

    <td><%= link_to 'remove', {:controller => 'inventories', :action => 'remove_cart_item', :id => "#{item.getinventoryid}"} %></td>
    

    到 CartItem.rb 模型

    将 attr_reader :inventory, :quantity 更改为 attr_accessor :inventory, :quantity

    def getinventoryid
       @inventory.id
    end
    

    到 Cart.rb 模型:

    将 attr_reader :items 更改为 attr_accessor :items

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

    到inventories_controller.rb:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      相关资源
      最近更新 更多