在导航栏 Rails 应用中显示购物车商品数量而不是购物车总价
Showing Number of Cart Items instead of Cart total price in navbar Rails app
在我的 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 %>
它显示 @cart.total_price
但我希望它显示购物车中的总项目数。
我不知道该怎么做
此代码的大部分来自 Udemy 教程,但就我的经验而言,它变得有点太多了,所以我感到很迷茫,试图根据我的需要修改它。
我一直在尝试将 @product_item
添加到上面的代码中,但没有成功,任何人都可以看看这个并指导我完成这个。
我在 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
在 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
在 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
然后我有 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
相关型号为cart.rb
和product_item.rb
在 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
并且在 product_item.rb
中有这样的代码:
class ProductItem < ActiveRecord::Base
belongs_to :product
belongs_to :cart
belongs_to :order
def total_price
product.price * quantity
end
end
如果您想要购物车中的全部商品,只需在 Cart
模型中对 product_items
列的 quantity
列求和,如下所示:
def total_quantity
product_items.sum(:quantity)
end
然后您可以从视图(或其他任何地方)调用 @cart.total_quantity
。
请注意,通过在 product_items
上调用 sum
,这是一个关系(而不是数组),这将直接使用 SQL:
计算总和
SELECT SUM(`product_items`.`quantity`) FROM product_items
WHERE `product_items`.`cart_id` = 123
其中 123
是购物车的 ID。
这比在 数组 产品项(即 product_items.to_a.sum(&:quantity)
)上调用 sum
更有效,后者会加载每个产品项,然后求和数量。但两者都应该有效,并且取决于你在做什么,后者 sum
可能更适合你的需要。
您可以重写 _navbar.html.erb
中的 @cart
代码,使其看起来像这样,它应该像前一个块一样工作,但显示 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 %>
在我的 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 %>
它显示 @cart.total_price
但我希望它显示购物车中的总项目数。
我不知道该怎么做
此代码的大部分来自 Udemy 教程,但就我的经验而言,它变得有点太多了,所以我感到很迷茫,试图根据我的需要修改它。
我一直在尝试将 @product_item
添加到上面的代码中,但没有成功,任何人都可以看看这个并指导我完成这个。
我在 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
在 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
在 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
然后我有 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
相关型号为cart.rb
和product_item.rb
在 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
并且在 product_item.rb
中有这样的代码:
class ProductItem < ActiveRecord::Base
belongs_to :product
belongs_to :cart
belongs_to :order
def total_price
product.price * quantity
end
end
如果您想要购物车中的全部商品,只需在 Cart
模型中对 product_items
列的 quantity
列求和,如下所示:
def total_quantity
product_items.sum(:quantity)
end
然后您可以从视图(或其他任何地方)调用 @cart.total_quantity
。
请注意,通过在 product_items
上调用 sum
,这是一个关系(而不是数组),这将直接使用 SQL:
SELECT SUM(`product_items`.`quantity`) FROM product_items
WHERE `product_items`.`cart_id` = 123
其中 123
是购物车的 ID。
这比在 数组 产品项(即 product_items.to_a.sum(&:quantity)
)上调用 sum
更有效,后者会加载每个产品项,然后求和数量。但两者都应该有效,并且取决于你在做什么,后者 sum
可能更适合你的需要。
您可以重写 _navbar.html.erb
中的 @cart
代码,使其看起来像这样,它应该像前一个块一样工作,但显示 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 %>