【问题标题】:Calculating an order total in rails计算rails中的订单总数
【发布时间】:2014-05-04 13:38:42
【问题描述】:

我对 Rails 还很陌生,到目前为止,我正在尝试计算订单总数,但运气不佳。我没有篮子或购物车模型,而是将这些物品放在篮子会话中。我想在创建订单之前在购物篮中的产品视图中显示总价。

我有一个 Order 模型、一个 Product 模型和一个 Order_Product 模型。

在我的订单模型中,我有以下关系:

has_many :order_products
has_many :products, through: :order_products

在我的产品模型中,我有以下关系:

has_many :order_products
has_many :orders, through: :order_products

在我的 Order_Product 模型中,我有以下关系:

  belongs_to :order
  belongs_to :product

在我的订单模型中,我有以下方法:

def total
    products.sum(&:price)
end 

在我看来:

<%= @order.total%>

我有一个带有以下内容的篮子控制器:

def create


    product = Product.find(params[:product_id])

    basket.add(product.id)

    flash[:success] = "Product added to basket"

    redirect_to product_path(product)

end

还有一个带有这个的订单控制器:

def new

@order = Order.new

    basket.each do |item_id|
        @order.order_products.build(product: Product.find(item_id))
    end

end

这是我的应用程序控制器中的购物篮会话:

def basket
session[:basket] ||= Set.new
end

没有错误被抛出,但视图中呈现的总数为“0”,尽管订单中有项目。由于我不知道该怎么做,有人可以帮忙吗?

非常感谢

【问题讨论】:

  • 您在 Rails 控制台中尝试过吗?如果不是先尝试那里,调试..
  • 我不确定如何在控制台中调试,但是当我在视图中使用调试方法时,它给出的只是这个“--- 0 ...”它只是在之前添加了破折号和点在零之后。
  • Ok.. 就在你的项目根目录中.. 输入rails c...试试Order.all.. 然后Order.first.products.sum(&amp;:price) 等等,,, 这是你应该首先调试代码的方式...
  • 价格属性的类型是什么?
  • 好吧..因为您处于开发模式(当您以 rails c 启动控制台时).. 可能是您的 products 表是空的.. 输出是什么Product.all.size 的?

标签: ruby-on-rails ruby-on-rails-4


【解决方案1】:

没有错误被抛出,但视图中呈现的总数为“0”,尽管订单中有项目。

选定的products 位于basket(存储在会话中),尚未与order 关联。因此,总数呈现为0

从您的 cmets 中获取线索。 根据您对 Babar 评论的回复:

Babar:我相信您想在创建订单之前在您的视图中显示购物篮中产品的总价格,对吧?詹姆斯:是的 完全正确!

为了在您的视图中显示购物篮中产品的总价格,您可以在app/helpers/products_helper.rb 文件中添加一个辅助方法。这样,该方法将自动在 app/views/products 目录下的所有视图中可用。

module ProductsHelper
  def basket_total
    Product.where(id: basket.to_a).sum(:price)
  end
end

另外,将来如果您计划在整个站点的所有页面的标题中显示 mini-basket,那么最好将辅助方法移动到 ApplicationHelper 模块 (app/helpers/application_helper.rb) 中,这样它会自动在您的应用程序中的所有视图中都可用。

module ApplicationHelper
  def basket_total
    Product.where(id: basket.to_a).sum(:price)
  end
end 

接下来,最好将basket 方法移动到ApplicationHelper 中,这样basket_total 以及您的视图都可以访问它们。

因此,您更新后的 ApplicationHelper 应该如下所示:

module ApplicationHelper

  def basket_total
    Product.where(id: basket.to_a).sum(:price)
  end

  def basket
    session[:basket] ||= Set.new
  end

end  

为了使ApplicationHelper 中的方法在所有控制器中可用,您需要更新ApplicationController (app/controllers/application_controller.rb),如下所示:

class ApplicationController < ActionController::Base
  include ApplicationHelper  ## Add this
  ## ...
end

最后,要访问视图中的basket_total 方法,请使用

<%= basket_total %>

【讨论】:

  • 如果我按照您的解释执行上述所有操作,我的 Order 模型中是否也可以使用 basket_total 方法?
  • basket_total 将仅在所有控制器(OrdersController、ProductsController、BasketsController 等)和应用程序中的所有视图中可用,但在模型中不可用。
【解决方案2】:

Kirti's answer is better use that

创建一个辅助方法解决了这个问题,代码是:
def total
    price=Product.find(session[:basket].to_a).collect{|product| product.price}.sum
end

【讨论】:

  • +1 感谢您的努力,并为帮助新手付出更多努力。我希望我知道我可以加入的你们的 skype id。
  • 非常感谢,非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-24
  • 2020-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多