Ruby on rails 计算rails中的订单总数

Ruby on rails 计算rails中的订单总数,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我是rails的新手,我正在尝试计算一个订单总数,但目前为止运气不好。我没有篮子或购物车模型,而是在篮子会话中保存项目。在创建订单之前,我想在视图中显示篮子中产品的总价 我有一个订单模型,一个产品模型和一个订单产品模型 在我的订单模型中,我有以下关系: has_many :order_products has_many :products, through: :order_products has_many :order_products has_many :orders, through:

我是rails的新手,我正在尝试计算一个订单总数,但目前为止运气不好。我没有篮子或购物车模型,而是在篮子会话中保存项目。在创建订单之前,我想在视图中显示篮子中产品的总价

我有一个订单模型,一个产品模型和一个订单产品模型

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

has_many :order_products
has_many :products, through: :order_products
has_many :order_products
has_many :orders, through: :order_products
  belongs_to :order
  belongs_to :product
在我的产品模型中,我有以下关系:

has_many :order_products
has_many :products, through: :order_products
has_many :order_products
has_many :orders, through: :order_products
  belongs_to :order
  belongs_to :product
在我的订单产品模型中,我有以下关系:

has_many :order_products
has_many :products, through: :order_products
has_many :order_products
has_many :orders, through: :order_products
  belongs_to :order
  belongs_to :product
在我的订单模型中,我有以下方法:

def total
    products.sum(&:price)
end 
我认为:

<%= @order.total%>
以及具有以下功能的订单控制器:

def new

@order = Order.new

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

end
以下是我的应用程序控制器中的basket会话:

def basket
session[:basket] ||= Set.new
end
不会抛出任何错误,但视图中呈现的总数为“0”,尽管顺序中存在项。我不知道该怎么办,谁能帮我一把

非常感谢

不会抛出任何错误,但视图中呈现的总数为“0”,尽管顺序中存在项

所选的
产品
篮子中(存储在会话中),尚未与
订单
关联。因此,总数呈现为
0

抓住你评论中的线索。 根据您对巴巴尔评论的回复:

巴巴尔:我相信在创建订单之前,您希望在您的视图中显示篮子中产品的总价格,对吗?詹姆斯:是的 完全正确

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

module ProductsHelper
  def basket_total
    Product.where(id: basket.to_a).sum(:price)
  end
end
此外,将来如果您计划在整个站点的所有页面标题中显示
迷你篮
,那么最好在
ApplicationHelper
模块(
app/helpers/application\u helper.rb
中移动helper方法)因此,它将自动在应用程序中的所有视图中可用

module ApplicationHelper
  def basket_total
    Product.where(id: basket.to_a).sum(:price)
  end
end 
接下来,最好在
ApplicationHelper
中移动
basket
方法,以便
basket\u 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\u controller.rb
),如下所示:

class ApplicationController < ActionController::Base
  include ApplicationHelper  ## Add this
  ## ...
end
不会抛出任何错误,但视图中呈现的总数为“0”,尽管顺序中存在项

所选的
产品
篮子中(存储在会话中),尚未与
订单
关联。因此,总数呈现为
0

抓住你评论中的线索。 根据您对巴巴尔评论的回复:

巴巴尔:我相信在创建订单之前,您希望在您的视图中显示篮子中产品的总价格,对吗?詹姆斯:是的 完全正确

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

module ProductsHelper
  def basket_total
    Product.where(id: basket.to_a).sum(:price)
  end
end
此外,将来如果您计划在整个站点的所有页面标题中显示
迷你篮
,那么最好在
ApplicationHelper
模块(
app/helpers/application\u helper.rb
中移动helper方法)因此,它将自动在应用程序中的所有视图中可用

module ApplicationHelper
  def basket_total
    Product.where(id: basket.to_a).sum(:price)
  end
end 
接下来,最好在
ApplicationHelper
中移动
basket
方法,以便
basket\u 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\u controller.rb
),如下所示:

class ApplicationController < ActionController::Base
  include ApplicationHelper  ## Add this
  ## ...
end
不会抛出任何错误,但视图中呈现的总数为“0”,尽管顺序中存在项

所选的
产品
篮子中(存储在会话中),尚未与
订单
关联。因此,总数呈现为
0

抓住你评论中的线索。 根据您对巴巴尔评论的回复:

巴巴尔:我相信在创建订单之前,您希望在您的视图中显示篮子中产品的总价格,对吗?詹姆斯:是的 完全正确

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

module ProductsHelper
  def basket_total
    Product.where(id: basket.to_a).sum(:price)
  end
end
此外,将来如果您计划在整个站点的所有页面标题中显示
迷你篮
,那么最好在
ApplicationHelper
模块(
app/helpers/application\u helper.rb
中移动helper方法)因此,它将自动在应用程序中的所有视图中可用

module ApplicationHelper
  def basket_total
    Product.where(id: basket.to_a).sum(:price)
  end
end 
接下来,最好在
ApplicationHelper
中移动
basket
方法,以便
basket\u 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