I'm working my way through creating the depot app from the book 'agile web development with rails'. I'm wanting to change its functionality so that instead of the cart appearing in the side column, I instead get a statement with '(x) items currently in your cart'.
I've got this code:
line_items controller (items that are in the cart):
def create
@cart = current_cart
product = Product.find(params[:product_id])
@line_item = @cart.add_product(product.id)
respond_to do |format|
if @line_item.save
format.html { redirect_to store_url }
format.js { @current_item = @line_item }
format.json { render json: @line_item,
status: :created, location: @line_item }
else
format.html { render action: "new" }
format.json { render json: @line_item.errors,
status: :unprocessable_entity }
end
end
end
and the carts controller:
def show
begin
@cart = Cart.find(params[:id])
rescue ActiveRecord::RecordNotFound
logger.error "Attempt to access invalid cart #{params[:id]}"
redirect_to store_url, notice: 'Invalid cart'
else
respond_to do |format|
format.html # show.html.erb
format.json { render json: @cart }
end
end
end
How do I reference the cart from the application layout view so that I can change the '(x) items' to the current number of items in the cart? I've tried @total_cart.line_items
and all other variations I can think of.
EDIT: I've got code in the cart model that has current_item.quantity
- how would I reference this in the application layout view, as this is the value I want? Thanks!