[英]Showing Number of Cart Items instead of Cart total price in navbar Rails app
In my rails app I have this code in my _navbar.html.erb
在我的rails应用程序中,我的
_navbar.html.erb
有此代码
<% if @cart.total_price > 0 %>
<%= link_to @cart do %>
<i class="fa fa-shopping-cart"> € </i>
<%= @cart.total_price %>
<% end %>
<% else %>
<a href="#" class="menu-cart"><i class="fa fa-shopping-cart"></i> € 0.00</a>
<% end %>
It displays the @cart.total_price
but I want it to display the total items in the Cart instead. 它显示
@cart.total_price
但我希望它显示购物车中的全部项目。
I'm not sure how to do that 我不确定该怎么做
Most of this code is from a Udemy tutorial, but it's getting little bit to much for my experience so I feel kind a lost trying to modify this to my needs. 这段代码大部分来自Udemy教程,但是根据我的经验来说,它变得无关紧要,因此我对尝试根据自己的需求进行修改感到迷失。
I've been trying to add the @product_item
to the above code but with out a luck, can anyone take a look at this and guide me through this. 我一直在尝试将
@product_item
添加到上面的代码中,但是很幸运,任何人都可以看看这个并指导我完成这个过程。
I have this code in the application_controller.rb
我在
application_controller.rb
有此代码
before_action :set_cart
def set_cart
@cart = Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
@cart = Cart.create
session[:cart_id] = @cart.id
end
In the carts_controller.rb
I have this: 在
carts_controller.rb
我有这个:
class CartsController < ApplicationController
before_action :set_cart, only: [:show, :destroy]
rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
def new
@cart = Cart.new
end
def show
@images = ["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg"]
@random_no = rand(5)
@random_image = @images[@random_no]
end
def destroy
@cart.destroy if @cart.id == session[:cart_id]
session[:cart_id] = nil
redirect_to root_url, notice: 'Your Cart is Empty'
end
private
def set_cart
@cart = Cart.find(params[:id])
end
def cart_params
params[:cart]
end
def invalid_cart
logger_error = 'You are trying to access invalid cart'
redirect_to root_url, notice: 'Invalid Cart'
end
end
In the controllers/concerns
I've this file current_cart.rb
在
controllers/concerns
我有此文件current_cart.rb
module CurrentCart
private
def set_cart
@cart = Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
@cart = Cart.create
session[:cart_id] = @cart.id
end
end
Then I have the product_items_controller.rb
and there is this code: 然后,我有了
product_items_controller.rb
并且有以下代码:
class ProductItemsController < ApplicationController
include CurrentCart
before_action :set_cart, only: [:create]
before_action :set_product_item, only: [:show, :destroy]
def create
@product = Product.find(params[:product_id])
@product_item = @cart.add_product(@product.id)
if @product_item.save
redirect_to root_url, notice:'Product added to Cart'
else
render :new
end
end
private
def set_product_items
@product_item = ProductItem.find(params[:id])
end
def product_item_params
params.require(:product_item).permit(:product_id)
end
end
And the relevant models are cart.rb
and product_item.rb
和相关的模型是
cart.rb
和product_item.rb
in the cart.rb
I have this code: 在
cart.rb
我有以下代码:
class Cart < ActiveRecord::Base
has_many :product_items, dependent: :destroy
def add_product(product_id)
current_item = product_items.find_by(product_id: product_id)
if current_item
current_item.quantity += 1
else
current_item = product_items.build(product_id: product_id)
end
current_item
end
def total_price
product_items.to_a.sum{|item| item.total_price}
end
end
And in the product_item.rb
there is this code: 在
product_item.rb
有以下代码:
class ProductItem < ActiveRecord::Base
belongs_to :product
belongs_to :cart
belongs_to :order
def total_price
product.price * quantity
end
end
You can rewrite the @cart
code in the _navbar.html.erb
to look like this, it should work just like the previous chunk but shows the product_items.count
instead of the total_price
你可以重写
@cart
代码在_navbar.html.erb
看起来是这样,它应该工作就像前面的块,表示的是product_items.count
代替的total_price
<% if @cart.product_items.count > 0 %>
<%= link_to @cart do %>
<i class="fa fa-shopping-cart"></i>
<%= @cart.product_items.count %>
<% end %>
<% else %>
<a href="#" class="menu-cart"><i class="fa fa-shopping-cart"></i> 0 </a>
<% end %>
If you want total items in the cart, just take the sum of the quantity
column over product_items
in your Cart
model, like this: 如果要在购物车中添加商品总数,只需将
Cart
模型中product_items
上的quantity
列的总和取为:
def total_quantity
product_items.sum(:quantity)
end
Then you can just call @cart.total_quantity
from the view (or anywhere else). 然后,您可以从视图(或其他任何位置)中调用
@cart.total_quantity
。
Note that by calling sum
on product_items
, which is a relation (and not an array), this directly calculates the sum using SQL: 请注意,通过在
product_items
上调用sum
,它是一个关系(而不是数组),这将使用SQL直接计算和:
SELECT SUM(`product_items`.`quantity`) FROM product_items
WHERE `product_items`.`cart_id` = 123
where 123
is the id of the cart. 其中
123
是购物车的ID。
This is more efficient than calling sum
on an array of product items (ie product_items.to_a.sum(&:quantity)
), which would load each product item, then sum the quantities. 这比对产品项数组 (即
product_items.to_a.sum(&:quantity)
)调用sum
效率更高,后者将加载每个产品项,然后对数量求和。 But either should work, and depending on what you're doing the latter sum
may suit your needs better. 但无论哪种应该工作,并根据你在做什么,后者
sum
可能会满足您的需求更好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.