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

  • Home
  • SEARCH
  • 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 613891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:03:09+00:00 2026-05-13T18:03:09+00:00

I have a cart which contains items, in my controller index method I use

  • 0

I have a cart which contains items, in my controller index method I use @cart = find_cart to find my cart’s items.

I’m trying to make a simple cart link which contains the amount of items in the cart at the top of my application layout using: <%= @cart.items.length %> It will look like cart(2), if you have two items.

Without being repetitive (that is adding @cart = find_cart to every single controller method) how do I efficiently make this data available across my entire application?

  • 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-05-13T18:03:09+00:00Added an answer on May 13, 2026 at 6:03 pm

    You will want to use a before_filter and place it in your Application Controller:

    class ApplicationController < ActionController::Base
        before_filter :find_cart_items
    
    
        private
        def find_cart_items
           @cart = find_cart
        end
    end
    

    Then, in any of your controllers where you do not want to find the cart, just use:

    skip_before_filter :find_cart_items
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 345k
  • Answers 345k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I also got this problem when using a local page… May 14, 2026 at 5:49 am
  • Editorial Team
    Editorial Team added an answer You can easily generate code using templates based on stored… May 14, 2026 at 5:49 am
  • Editorial Team
    Editorial Team added an answer I would suggest you have a look at Windows Powershell,… May 14, 2026 at 5:49 am

Related Questions

Let's say I have I have an online store with a shopping cart feature
I have a header.html which start with session_start();. Then the following code, $_SESSION['cart'][$sw_id] is
I have a page in a asp.net mvc shopping cart which allows you to
I have two classes, one for articles and another for the cart. The cart
I ran into an interesting (and very frustrating) issue with the equals() method today

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.