【问题标题】:Quantity in Cart Model Basic Logic Ruby on Rails购物车中的数量 模型基本逻辑 Ruby on Rails
【发布时间】:2012-11-03 15:18:12
【问题描述】:

我正在尝试将一些非常基本的逻辑应用到我的观点中,但到目前为止还失败了。

如果有以下情况,我想做的是返回 item.discount_price:

  1. item.product_id 等于 1
  2. Cart.item.quantity 计数等于或大于 2。

目前我有以下:

物品模型

class Item < ActiveRecord::Base
belongs_to :product
belongs_to :cart

def price_check

    if  Item.product_id = 1 && Item.quantity.count >= 2 
        return Item.discount_price
    else
        return Item.unit_price
    end 

end

结束

查看

<% for item in @cart.items %>
<tr class="<%= cycle :odd, :even %>">
  ...
  <td class="price"><%= gbp(item.price_check) %></td>
  ...
</tr>

关联如下:

Cart - has_many :items
Items - Belongs_to :cart and :products
Products - has_ many :items

我不断收到的错误:

 NoMethodError in Carts#show

Showing C:/Sites/checkout/app/views/carts/show.html.erb where line #12 raised:

undefined method `quantity' for #<Class:0x50c3058>

Extracted source (around line #12):

9:        <tr class="<%= cycle :odd, :even %>">
10:       <td><%=h item.product.name %></td>
11:       <td class="qty"><%= item.quantity %></td>
12:       <td class="price"><%= gbp(item.price_check) %></td>
13:       <td class="price"><%= gbp(item.full_price) %></td>
14:       <td><%= button_to 'Remove', item, :method => :delete %></td>
15:       </tr>

app/models/item.rb:12:in `price_check'
app/views/carts/show.html.erb:12:in `block in _app_views_carts_show_html_erb___389237738_48997308'
app/views/carts/show.html.erb:8:in `_app_views_carts_show_html_erb___389237738_48997308'

任何人可以提供解决此问题的帮助将不胜感激!谢谢E

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 if-statement model logic


    【解决方案1】:

    RadBrad 有正确的想法,但实施错误

    def price_check
      # Product Discount for Lavender Heart (Product code 001, greater than 2 in cart)        
      # Thought: Shouldn't you check to see if the name of the item is "Lavender Heart"?
      #          Checking if the product_id is 1 makes this test brittle
      if product_id == 1 && cart.items.quantity.count >= 2 
        discount_price
      else
        unit_price
      end 
    end
    

    【讨论】:

    • 非常感谢您的帮助杰森!只有一个简单的问题,如果折扣价格存储在 Product 表(而不是 unit_price 所在的 Item 表)中,我如何正确引用 discount_price?
    • 在您的 Item 模型中,添加以下行:delegate :discount_price, :to => :product
    【解决方案2】:

    第一个明显的问题是这样的:

    if  Item.product_id = 1 && Item.quantity.count >= 2 
    

    您要求调用 CLASS 方法 product_id。你想要实例方法,即

    if @item.product_id == 1 && @item.quantity >= 2
    

    通常会在前面加上:

    @item = Item.find(params[:id])
    

    【讨论】:

    • 感谢您的快速回复。我遇到的问题是,使用实例方法时,我不断收到诸如undefined method product_id=' for nil:NilClass` 之类的错误,如果我在前面加上@item = Item.find(params[:id]),那么我会得到undefined local variable or method params'` 你可以提供的任何进一步帮助真的会不胜感激!
    • 参数仅在控制器中可用。在您看来,您正在遍历购物车项目并在每个项目上调用实例方法 (price_check)。实例方法应该只询问关于自己的事情(我的 product_id == 1 并且我的购物车中是否有两个以上的商品),并返回商品的 discount_price 或 unit_price。我上面的答案应该有效。
    猜你喜欢
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    相关资源
    最近更新 更多