2016-09-29 68 views
0

在我的Rails應用程序,我有這樣的代碼在我_navbar.html.erb顯示車項目,而不是在導航欄的Rails應用車總價數

  <% if @cart.total_price > 0 %> 
       <%= link_to @cart do %> 
        <i class="fa fa-shopping-cart"> &euro; </i> 
       <%= @cart.total_price %> 
       <% end %> 

       <% else %> 
        <a href="#" class="menu-cart"><i class="fa fa-shopping-cart"></i> &euro; 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.rbproduct_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 
+1

不應該只是被總結所有與'cart'關聯的所有'product_items'的數量? – lusketeer

+0

是的,但我不確定如何將其寫入導航欄代碼中......目前,由於缺乏紅寶石體驗,此操作很複雜 – Slowboy

+0

'<%= @ cart.products.count%>' –

回答

1

您可以重寫在_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 %>  
0

如果你想總計在車中的物品,只取quantity列在product_items總和在Cart模型中,像這樣:

def total_quantity 
    product_items.sum(:quantity) 
end 

然後,你可以調用從視圖@cart.total_quantity(或其他地方)。

注意,通過調用sumproduct_items,這是一個關係(而不是一個陣列),這直接計算的總和使用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可能更適合你的需求。