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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:34:48+00:00 2026-05-27T08:34:48+00:00

Trying to build a shopping cart based on the Agile Web Development 4th edition.

  • 0

Trying to build a shopping cart based on the Agile Web Development 4th edition. The shopping cart is for selling t-shirts. The t-shirts have various size options, color options, and gender options. I want users to be able to select their options from lists, click “add to cart”, and have the matching t-shirt added to to the cart (in the form of a line_item).

I think I am SO close to getting this done. But not quite. Here are the relevant code snippets:

The Form (/_store.html.erb):

<div id="storefunctions">
<%= form_tag '/line_items/create', :id => "tshirt" do %>
<div>
    size:&nbsp;<%= render 'size_select' %>&nbsp;
    color:&nbsp;<%= render 'color_select' %>&nbsp;
    gender:&nbsp;<%= render 'gender_select' %>
    <%= hidden_field_tag(:message_content, @message.content) %></div
 <div> <%= submit_tag 'add to cart'  %></div>
<% end %>   

Line_items Controller (/line_items/create.rb)

def create
@cart      = current_cart
tshirt     = Tshirt.where("size = ? AND gender = ? AND color = ?", params[:size], params[:gender], params[:color])
@line_item = @cart.line_items.build(:tshirt_id => tshirt.id)

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

end

When I click “add to cart” it says I have successfully added it to cart, but I can not access any information about the shirt. It says it is a nil.

Any help would be 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-27T08:34:48+00:00Added an answer on May 27, 2026 at 8:34 am

    Model.where(...) is going to be an array of results, not a single result object, so you can’t call .id on it. You probably want Tshirt.where("size = ? AND gender = ? AND color = ?", params[:size], params[:gender], params[:color]).first

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

Sidebar

Related Questions

I am trying to build a shopping cart site. When a user click add
I am trying to build a simple shopping cart, i read about controlling them
I am trying to build a shopping website with ajax. When a user clicks
I have a std::vector< tr1::shared_ptr<mapObject> > that I'm trying build from data contained in
Hi i was recently looking over a shopping cart paginator class, trying to understand
I am trying build small webcam chat (web application). I searched on google and
I'm storing my shopping cart in the database and have a current_cart method in
I'm trying to build a master page. What I'm trying to do is have
I am a fairly new to ROR. I'm trying to build my first web
I'm trying to style a shopping cart generated by a system so I can't

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.