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 6559945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:23:47+00:00 2026-05-25T13:23:47+00:00

Im super new to programming and I’m trying to get a shopping cart working

  • 0

Im super new to programming and I’m trying to get a shopping cart working in my app which is linked to user sessions. So each user can have their own shopping cart and no one can view anyone else’s cart.

I have a working cart thanks to Railscasts but its currently being created in its own session. So it doesn’t matter if you log in as different users, there is only ever one cart in use and all user share it.

Its currently being created as such:

Application controller

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details
  helper_method :current_user
  helper_method :current_cart

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

  def current_cart
    if session[:cart_id]
      @current_cart ||= Cart.find(session[:cart_id])
      session[:cart_id] = nil if @current_cart.purchased_at
    end
    if session[:cart_id].nil?
      @current_cart = Cart.create!
      session[:cart_id] = @current_cart.id
    end
    @current_cart
  end
end

Line items controller

class LineItemsController < ApplicationController
  def create
    @product = Product.find(params[:product_id])
    @line_item = LineItem.create!(:cart => current_cart, :product => @product, :quantity => 1, :unit_price => @product.price)
    flash[:notice] = "Added #{@product.name} to cart."
    redirect_to current_cart_url
  end
end

I’ve got as far as adding a user_id to the cart model and setting user has_one cart and a cart belong_to a user but I can’t figure out what needs to change in the way a cart is created to actually get it to work.

edit – Sessions controller

  def create
    user = User.authenticate(params[:username], params[:password])
    if user
      session[:user_id] = user.id
      current_cart.user = current_user
      current_cart.save
      redirect_to root_path, :notice => "Welcome back!"
    else
      flash.now.alert = "Invalid email or password"
      render "new"
    end
  end

  def destroy
    session[:user_id] = nil
    redirect_to root_path, :notice => "Logged out!"
  end

Any help is very much appreciated!

  • 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-25T13:23:48+00:00Added an answer on May 25, 2026 at 1:23 pm

    The cart is tied to the session, so not all users will share it, it will be unique to the browser session it was created in – essentially one cart for every browser session created that visits your LineItemsController#create method.

    This is typically done to allow a cart to be created before a user logs in or signs up, reducing the friction in actually adding items to a cart.

    If you want to associate the cart with a user, then you could do this when they log in or sign up. If you’ve added the relation, then this should be as simple as:

    current_cart.user = current_user
    current_cart.save
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im super new to programming and trying to get to grips with rails, I'm
im new to iPhone programming..in my app the timer is not working fine. can
I'm super new to programming, I currently have a table of venues which can
First off I'm new at programming. I'm creating an app with one navigation bar
I'm working on a small Java applet, very much new to this programming thing.
I am new to programming and my app crashes every time I try to
Hi I'm new to android programming, I'm trying to initiate the on click method
I'm super new to programming and rails. I have a table of venues and
I'am begginer in android programming and I'am trying to create app that is logging
New to android programming. I have a layout XML, to which I want to

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.