私は自分の見解にいくつかの非常に基本的なロジックを適用しようとしていますが、これまでのところ失敗しています。
次の条件がある場合は、item.discount_priceを返品します。
- item.product_idは1に等しい
- 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