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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:59:37+00:00 2026-06-05T09:59:37+00:00

See Update’s 1 & 2 below for Solution I’ve been following the tutorial on

  • 0

See Update’s 1 & 2 below for Solution

I’ve been following the tutorial on creating a shopping cart from the book “Agile Web Development with Rails”..They have the chapter I’ve been following here: http://media.pragprog.com/titles/rails4/cart.pdf

In the book they have you create a line_item that get’s passed to a cart and stored in the session by using this line <%= button_to ‘Add to Cart’, line_items_path(product_id: product) %> This in my case would obviously be changed to <%= button_to ”, line_items_path(gear_id: gear), id: ‘rent_it’ %>

If I want to change this so it’s a form that also accepts other parameters how would I do this?

Below is my code that I’ve used and attempted to accomplish this but so far unsuccessfully.
With the below code I get and error that can’t find gear without ‘id’. I’m new to rails and programming in general so I appreciate the help.

UPDATE 1 (I updated my View below as well)

I can now see from my server logs that I’m passing the ID of the Gear in the form. Which is what I was originally trying to do.

Processing by LineItemsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"0QnJShABOV+avPx0AJHxuMmskfwtwFlkpMa6cBhTU3s=", "line_item"=>{"rentstart"=>"", "rentend"=>"", "gear_id"=>"13"}, "commit"=>""}
  Cart Load (0.4ms)  SELECT `carts`.* FROM `carts` WHERE `carts`.`id` = 18 LIMIT 1
Completed 500 Internal Server Error in 2ms

ActiveRecord::RecordNotFound (Couldn't find Gear without an ID):
  app/controllers/line_items_controller.rb:44:in `create'

I’m still getting the error Couldn’t find Gear without an ID. Which are the 2 lines below:

    gear = Gear.find(params[:gear_id])
    @line_item = @cart.add_gear(gear.id)

What am I missing?

Line Item’s Controller

  def create
    @cart = current_cart
    gear = Gear.find(params[:gear_id])
    @line_item = @cart.add_gear(gear.id)

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to @line_item.cart }
        format.json { render json: @line_item, status: :created, location: @line_item }
      else
        format.html { render action: "new" }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end

Cart Model

class Cart < ActiveRecord::Base
  has_many :line_items, dependent: :destroy

  def add_gear(gear_id)
    current_item = line_items.find_by_gear_id(gear_id)
    if current_item
      current_item.quantity += 1
    else
      current_item = line_items.build(params[:line_item])
    end
    current_item
  end

  def total_price
    line_items.to_a.sum { |item| item.total_price }
  end

end

View from Gear Show Page

<div class="gearside_date_main">
        <h3>Rental Date</h3>
        <script>
        $(function() {
            var dates = $( "#rentstart, #rentend" ).datepicker({
                defaultDate: "+1w",
                changeMonth: true,
                numberOfMonths: 1,
                onSelect: function( selectedDate ) {
                    var option = this.id == "rentstart" ? "minDate" : "maxDate",
                        instance = $( this ).data( "datepicker" ),
                        date = $.datepicker.parseDate(
                            instance.settings.dateFormat ||
                            $.datepicker._defaults.dateFormat,
                            selectedDate, instance.settings );
                    dates.not( this ).datepicker( "option", option, date );
                }
            });
        });
        </script>


    <%= form_for LineItem.new do |f| %>
        <%= f.text_field :rentstart, id: 'rentstart'  %>
        <%= f.text_field :rentend, id: 'rentend'  %>
        <%= f.hidden_field :gear_id, :value => @gear.id %>
        <%= f.submit "", id: 'rent_it' %>
    <% end %>
        <% end %>
    </div>

UPDATE 2

I finally got it working after Murifox’s comments. I didn’t completely understand how the syntax is structured when passing a hash. So after much trial and error it is working. I’m including my updated code below to see the changes that I made. I know when I see answers I’m always looking for the change. I hope this helps someone else….

The View (Gear Show Page

<%= form_for LineItem.new do |f| %>
    <%= f.text_field :rentstart, id: 'rentstart'  %>
<%= f.text_field :rentend, id: 'rentend'  %>
<%= f.hidden_field :gear_id, :value => @gear.id %>
    <%= f.submit "", id: 'rent_it' %>
<% end %>

The Controller (Create Action)

  def create
    @cart = current_cart
    gear = Gear.find(params[:line_item][:gear_id])
    lineitem = params[:line_item]
    @line_item = @cart.add_gear(lineitem, gear.id)

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to @line_item.cart }
        format.json { render json: @line_item, status: :created, location: @line_item }
      else
        format.html { render action: "new" }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end

The Cart Model

  def add_gear(lineitem, gear_id)
    current_item = line_items.find_by_gear_id(gear_id)
    if current_item
      current_item.quantity += 1
    else
      current_item = line_items.build(lineitem)
    end
    current_item
  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-05T09:59:38+00:00Added an answer on June 5, 2026 at 9:59 am

    Try using

    gear = Gear.find(params[:line_item][:gear_id])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update Solution Found See Bottom of post if interested Seems simple enough and for
UPDATE (spoiler): This question is answered (see David Carlisle answere below) and it looks
UPDATE : I asked this question in another form (see below), and it got
Figured it out, see Update below. I'm trying to work with a particular web
(Important: See update below.) I'm trying to write a function, import_something , that will
UPDATE See bottom for update. I've been looking alot around the internet and I
See update below. This is driving me mad. I have followed all the instructions
Update: Please see below; I have removed all abstraction and created a plain PHP
Update - Please see update below. I'm attempting to improve the performance of our
See update below... I'm writing a Python simulation that assigns an arbitrary number of

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.