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 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

Related Questions

I have a simple cart page that displays items that are in someones cart,
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 created a simple shopping cart application. We needed something specific to our
I have a login.jsp page which contains a login form. Once logged in the
I have one list, which the item render it`s like this: link . But
I have a simple shopping cart web site that uses a MySQL database to
I have a flex datagrid with cart items populated from a service. Each row
Or vice versa. Update: Hmm, let's assume I have a shopping cart app, the
Let's say I have I have an online store with a shopping cart feature

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.