Ruby on rails Rails未定义的局部变量或方法'line_item';
您好,我在为我在网站中构建的购物车创建此条件时遇到了一点麻烦,该购物车在销售零件时显示总计。在我的模式中,我有零件、行_项,它们的id是零件和购物车,以及购物车。零件具有折扣属性。如果零件有折扣,它将显示折扣和零件的新价格。My line\u items有一个名为line\u item\u discount的方法,若一部分包含折扣,它将创建一个新的部分总和。虽然它显示了零件、折扣和新价格,但购物车总计不会更新它 我在这里创建了一个名为total_price_的方法,其中有折扣Ruby on rails Rails未定义的局部变量或方法'line_item';,ruby-on-rails,activerecord,undefined,Ruby On Rails,Activerecord,Undefined,您好,我在为我在网站中构建的购物车创建此条件时遇到了一点麻烦,该购物车在销售零件时显示总计。在我的模式中,我有零件、行_项,它们的id是零件和购物车,以及购物车。零件具有折扣属性。如果零件有折扣,它将显示折扣和零件的新价格。My line\u items有一个名为line\u item\u discount的方法,若一部分包含折扣,它将创建一个新的部分总和。虽然它显示了零件、折扣和新价格,但购物车总计不会更新它 我在这里创建了一个名为total_price_的方法,其中有折扣 class Car
class Cart < ActiveRecord::Base
has_many :order_items
belongs_to :user
has_many :line_items, dependent: :destroy
def add_part(part_id)
current_part = line_items.find_by(part_id: part_id)
if current_part
current_part.quantity += 1
else
current_part = line_items.build(part_id: part_id)
end
current_part
end
def total_price
line_items.to_a.sum { |item| item.total_price}
end
def total_price_with_discount
line_items.to_a.sum { |item| item.total_price.line_item_discount}
end
我的零件模型
class Part < ActiveRecord::Base
has_many :order_items
has_many :line_items
before_destroy :ensure_not_referenced_by_any_line_item
def ensure_not_referenced_by_any_line_item
if line_items.empty?
return true
else
errors.add(:base, 'Line Items present')
return false
end
end
def subtotal
parts.collect { |part| part.valid? ? (part.quantity * part.unit_price) : 0}.sum
end
def apply_discount
price - (discount.to_f/100 * price)
end
end
class部分
我的行项目模型
class LineItem < ActiveRecord::Base
belongs_to :part
belongs_to :cart
def total_price
part.price * quantity
end
def line_item_discount
part.price - (part.discount.to_f/100 * part.price) * quantity
end
end
class LineItem
这是抛出错误的局部视图
<h2>Your Cart</h2> <table>
<%= render(cart.line_items) %>
<tr class="total_line">
<td colspan="2">Total</td>
<%unless cart.line_items.part.discount?%>
<td class="total_cell"><%= number_to_currency(cart.total_price) %></td>
<%end%>
<%if cart.line_items.part.discount?%>
<td class="total_cell"><%= number_to_currency(cart.total_price_with_discount) %></td>
<%end%>
</tr>
</table>
<%= button_to 'Empty cart', cart, method: :delete, data: { confirm: 'Are you sure?' } %>
您的购物车
全部的
感谢您对此提供的帮助和建议。在您的视图中,您正在调用
购物车.line\u项目.part
,但是行项目
是多个行项目
对象的集合,。part
不是该集合对象上的有效方法
从错误中可以看出,ActiveRecord::Associations::CollectionProxy
缺少part
方法
您应该在LineItem
上创建一个作用域,如:
scope :with_discounted_part, { joins(:part).where.not(part: { discount: nil }) }
在您看来,您可以:
<% if cart.line_items.with_discounted_part %>
到
购物车
模型中,我将添加:
has_many :parts, through: :line_items
将范围添加到零件
模型:
scope :with_discounts, -> { where.not(discount: nil) }
def total_price
(part.price * quantity) - line_item_discount
end
def line_item_discount
return 0 if part.discount.blank?
(part.discount.to_f/100 * part.price) * quantity
end
然后将视图更改为:
<td class="total_cell">
<%if cart.parts.with_discount.any?%>
<%= number_to_currency(cart.total_price_with_discount) %>
<%else%>
<%= number_to_currency(cart.total_price) %>
<%end%>
</td>
那么您甚至不需要视图中的if语句,total\u price
将以任何方式工作:
<td class="total_cell"><%= number_to_currency(cart.total_price) %></td>
我使用的是你提供给我的底部策略,看起来这个代码实际上是以实际百分比作为价格,而不是折扣价格哦,等等,我很抱歉,我忘了从模型中取出带折扣的总价。你还必须在
行项目折扣中更改计算(我修改了答案中的代码)。它实际上是在计算折扣后的总价,而不是刚刚实现的折扣本身。
<td class="total_cell"><%= number_to_currency(cart.total_price) %></td>
def total_price
line_items.sum(:total_price)
end