Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8023773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:39:45+00:00 2026-06-04T22:39:45+00:00

I’ve created a shopping cart with line items that exist in the session object.

  • 0

I’ve created a shopping cart with line items that exist in the session object. I can’t figure out how I link to my shopping cart show page if the cart exists and just link to a ‘cart is empty’ page if it doesn’t. What am I missing? My code is below:

  • Cart has_many line_items, & lineitems belongs to Cart

View Link
, I wasn’t sure what I should put here and where to define it for example… “if current_cart.empty? cart”, or “if @cart.line_items.empty? current_cart”, etc.

        <% if current_cart.line_items.empty?%>
        <%= link_to "cart", cart_path, :class =>'headertab' %>&nbsp;
    <% else %>
        <%= link_to "cart", cart_path(current_cart), :class =>'headertab' %>&nbsp;
    <% end %>

This gives me the following error:

Showing /Users/dave/rails_projects/EquiptMe/app/views/layouts/_headerexterior.html.erb where line #20 raised:

undefined local variable or method `current_cart' for #<#<Class:0x007fae841cc8e0>:0x007fae83339698>
Extracted source (around line #20):

17:         <li><%= link_to "browse gear", '/gear', :class =>'headertab' %></li>
18:         <li><%= link_to "join", '/signup', :class =>'headertab' %></li>
19:         <li>
20:         <% if current_cart.line_items.empty?%>
21:             <%= link_to "cart", cart_path, :class =>'headertab' %>&nbsp;
22:         <% else %>
23:             <%= link_to "cart", cart_path(current_cart), :class =>'headertab' %>&nbsp;

Routes

Outdoor::Application.routes.draw do
  resources :line_items

  resources :carts
.....

Application Controller

class ApplicationController < ActionController::Base
  protect_from_forgery

  private

  def current_cart
    Cart.find(session[:cart_id])
    rescue ActiveRecord::RecordNotFound
      cart = Cart.create
      session[:cart_id] = cart.id
      cart
  end
end

Carts Controller “Show”, “Destroy” & “Create”

 def show
    begin
    @cart = Cart.find(params[:id])
    rescue ActiveRecord::RecordNotFound
      logger.error "Attempt to access invalid cart #{params[:id]}"
      redirect_to '/gear', notice: 'Invalid cart'
    else
      respond_to do |format|
        format.html # show.html.erb
        format.json { render json: @cart }
      end
   end

  def destroy
    @cart = current_cart
    @cart.destroy
    session[:cart_id] = nil

    respond_to do |format|
      format.html { redirect_to carts_url, notice: 'Your cart is currently empty' }
      format.json { head :no_content }
    end
end

  def create
    @cart = Cart.new(params[:cart])

    respond_to do |format|
      if @cart.save
        format.html { redirect_to @cart, notice: 'Cart was successfully created.' }
        format.json { render json: @cart, status: :created, location: @cart }
      else
        format.html { render action: "new" }
        format.json { render json: @cart.errors, status: :unprocessable_entity }
      end
    end
  end

Line Items Controller “Create”

 def create
    @cart = current_cart
    gear = Gear.find(params[:gear_id])
    @line_item = @cart.add_gear(gear.id)

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to @line_item.cart }
        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
  end

Thanks for the help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:39:48+00:00Added an answer on June 4, 2026 at 10:39 pm

    From your code, you always have a created Cart object. Your current_cart method, will always return new or previously created Cart object, so you have to link to cart_path(current_cart).

      <%= link_to "my cart", cart_path(current_cart), :class =>'headertab' %>
    

    In another case, if you want to check if the cart is empty? In this case you have to use current_cart.line_items.empty?

    Edited

    To access current_cart in Views and Controllers add helper_method :current_cart declaration to ApplicationController.

    Code update:

    class ApplicationController < ActionController::Base
      protect_from_forgery
    
      private
    
      def current_cart
        Cart.find(session[:cart_id])
        rescue ActiveRecord::RecordNotFound
          cart = Cart.create
          session[:cart_id] = cart.id
          cart
      end
      helper_method :current_cart
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.