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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:31:50+00:00 2026-05-22T03:31:50+00:00

I’m storing my shopping cart in the database and have a current_cart method in

  • 0

I’m storing my shopping cart in the database and have a current_cart method in my application helper.

 private

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

Each cart has_many line_items. When a customer is ready to check out, they are sent to order/new … when the order returns successful from the payment gateway though, everything is fine. But I’m testing the order failure and it seems there’s a problem with my create controller, because the @order object isn’t being saved and rails is trying to render order#new again. It’s not even getting to try and see if the payment was successful. I’m not sure what’ wrong, as it was working when I tested it a few days ago.

def create
    @order = current_cart.build_order(params[:order])
    @order.ip_address = request.remote_ip

    @order.total_tokens = @order.calculate_total_tokens
    @order.user_id = current_user

      if @order.save
        if @order.purchase
         render :action => "success"
        else
          render :action => "failure"
        end
      else
        render :action => "new"
      end
   end

 def new
    @cart = current_cart
    if @cart.line_items.empty?
        redirect_to store_url, :notice => "Your cart is empty."
        return
    end

     @order = Order.new

     respond_to do |format|
       format.html # new.html.erb
       format.xml  { render :xml => @order }
     end
  end

I’m getting this error when it tries to render the page, obviously because the order hasn’t been saved:

NoMethodError in Orders#create

Showing /Users/justin/Code/trackable/app/views/orders/_order_cart.html.erb where line #1     raised:

undefined method `line_items' for nil:NilClass
Extracted source (around line #1):

1: <% unless @cart.line_items.empty? %>
2: <div id="checkout_cart">
3: <% @cart.line_items.each do |l| %>
4:   <%= l.quantity %>

Not sure what’s going on

  • 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-22T03:31:50+00:00Added an answer on May 22, 2026 at 3:31 am

    So, adding @cart = current_cart to the #create controller seems to have fixed the problem. ActiveMerchant was throwing a validation error and for some reason, when it did that, it was losing @cart … it wasn’t doing that earlier when I was testing ActiveRecord validations, so I’m still not quite sure what I changed that screwed everything up.

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

Sidebar

Related Questions

I have a view passing on information from a database: def serve_article(request, id): served_article
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a reasonable size flat file database of text documents mostly saved in
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.