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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:47:26+00:00 2026-06-13T22:47:26+00:00

This one has me a perplexed. I’m still a newbie with Rails so it

  • 0

This one has me a perplexed. I’m still a newbie with Rails so it may be simple.

The situation:

  1. I can add items to the cart, no problem. Everything works fine the current_cart method resolves to the same cart each request.
  2. But as soon as I remove a line item from the cart, it works, the line item is deleted, but the
    variable “session[:cart_id]” becomes null, and a new cart gets
    created when current_cart is called the next time.

I’m using Devise so I’m not sure if that has something to do with it, or maybe the line item delete method is cascade deleting the session or something like that.

The question is, can anyone help me understand why the session variable is becoming null after calling line_item delete?

I’ve created a cart application, following along with Agile web development with rails. Firstly, here’s the code for application controller to retrieve the current cart:

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

Now the code for my line item controller

class Checkout::LineItemsController < ApplicationController

# POST /checkout/line_items # POST /checkout/line_items.json def
create

@cart = current_cart
product = Catalog::Product.find(params[:product_id])
apparel_event_log.info (product.to_json)


@checkout_line_item = @cart.line_items.find_or_initialize_by_sku_id(product.part_number)
new_quantity = @checkout_line_item.quantity || 0
@checkout_line_item.quantity = new_quantity+1
@checkout_line_item.line_item_description = product.name
@checkout_line_item.image = product.primary_image

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

# DELETE /checkout/line_items/1 # DELETE
/checkout/line_items/1.json def destroy
Checkout::LineItem.delete(params[:id])

respond_to do |format|
  format.html { redirect_to current_cart, notice: 'Line item removed.' }
  format.json { head :no_content }
end   end end

and also the code for the line items model

class Checkout::LineItem < ActiveRecord::Base
  before_save :default_values

  attr_accessible :customer_update_date, :inventory_status, :line_item_color, :line_item_description, :line_item_size, :line_item_tagline, :line_item_total, :image, :quantity, :sku_id, :style_id, :tax, :tax_code, :timestamps, :unit_price, :cart, :product
  belongs_to :cart
  belongs_to :product, :class_name => 'Catalog::Product'
  has_one :image, :class_name => 'Assets::Medium'

  def default_values
     Rails.logger.debug { "Entering default values" }
     self.quantity ||= 0
   end

end
  • 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-13T22:47:27+00:00Added an answer on June 13, 2026 at 10:47 pm

    Ok I actually managed to solve this now. That was tough!

    It appears to be some sort of problem using link_to … :destroy or delete, where I think default rails code wasn’t producing the CSRF protection and so was invalidating the session.

    I’ve solved the main part by replacing the link_to with button_to to delete the line item and that seems to be working, at least functionally.

    I really would prefer to use a link rather than a button. This seems to be a more common issue, so I’ll hunt the forums for an answer, but if you know an answer, appreciate if you could please post a link or the answer directly on this thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This one has got me stumped. I'm deploying a Rails 3 app to Slicehost
I know this one has been asked before, however I can't seem to find
This one has me stumped, since I don't think this can be done with
Ok, this one has me baffled, and I'm hoping some fresh perspective can lead
This one has me stumped. I've searched and found similar questions but I can't
This one has really got me confused: I have a group of 'items' and
This one has me perplexed. I have a web app that does many functions,
So, this one has me a little perplexed. I'm probably just looking at it
This one has me pretty rattled so I thank you in advance for your
This one has been stumping me for a while. But I'm no expert. This

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.