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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:58:44+00:00 2026-05-29T09:58:44+00:00

OK, so I am reading Agile Web Development with Rails and working on the

  • 0

OK, so I am reading “Agile Web Development with Rails” and working on the depot application. For those who have the book it is Chapter 9.3 Iteration D3: Adding a button.

Prior to this section we created a session to hold our “shopping cart”

class ApplicationController < ActionController::Base

  protect_from_forgery

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

Making it so any controller we create that extends ApplicationController has access to our cart.

In this section we edit the “create” function of the LineItemsController class which extends ApplicationControler.

The original create method looks like this

def create
  @line_item = LineItem.new(params[:line_item])

  respond_to do |format|
    if @line_item.save
      format.html { redirect_to @line_item, notice: 'Line item was successfully created.' }
      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 book says to modify if like this.

def create
  @cart = current_cart

  product = Product.find(params[:product_id])

  @line_item = @cart.line_items.build(product: product)

  respond_to do |format|
    if @line_item.save
      format.html { redirect_to @line_item.cart, notice: 'Line item was successfully created.' }
      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

I’ve triple checked the code. I have duplicated it from the book. Not being a ruby expert I don’t know what I am missing. The error it gives me is this.

ArgumentError in LineItemsController#create

wrong number of arguments (0 for 1)
Rails.root: /Users/jandrews/Projects/pragprog_ruby/depot

Application Trace | Framework Trace | Full Trace
activerecord (3.1.3) lib/active_record/relation.rb:318:in `destroy'
activerecord (3.1.3) lib/active_record/base.rb:442:in `destroy'
app/models/cart.rb:2:in `<class:Cart>'
app/models/cart.rb:1:in `<top (required)>'
activesupport (3.1.3) lib/active_support/dependencies.rb:456:in `load'
activesupport (3.1.3) lib/active_support/dependencies.rb:456:in `block in load_file'
activesupport (3.1.3) lib/active_support/dependencies.rb:640:in `new_constants_in'
activesupport (3.1.3) lib/active_support/dependencies.rb:455:in `load_file'
activesupport (3.1.3) lib/active_support/dependencies.rb:342:in `require_or_load'
activesupport (3.1.3) lib/active_support/dependencies.rb:489:in `load_missing_constant'
activesupport (3.1.3) lib/active_support/dependencies.rb:181:in `block in const_missing'
activesupport (3.1.3) lib/active_support/dependencies.rb:179:in `each'
activesupport (3.1.3) lib/active_support/dependencies.rb:179:in `const_missing'
activesupport (3.1.3) lib/active_support/dependencies.rb:501:in `load_missing_constant'
activesupport (3.1.3) lib/active_support/dependencies.rb:181:in `block in const_missing'
activesupport (3.1.3) lib/active_support/dependencies.rb:179:in `each'
activesupport (3.1.3) lib/active_support/dependencies.rb:179:in `const_missing'
app/controllers/application_controller.rb:7:in `current_cart'
app/controllers/line_items_controller.rb:43:in `create'
actionpack (3.1.3) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.1.3) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.1.3) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.1.3) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.1.3) lib/active_support/callbacks.rb:416:in `_run__1422100127945824718__process_action__3684163507935076470__callbacks'
activesupport (3.1.3) lib/active_support/callbacks.rb:386:in `_run_process_action_callbacks'
activesupport (3.1.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.3) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.1.3) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.1.3) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.1.3) lib/active_support/notifications.rb:53:in `block in instrument'
activesupport (3.1.3) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.1.3) lib/active_support/notifications.rb:53:in `instrument'
actionpack (3.1.3) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.1.3) lib/action_controller/metal/params_wrapper.rb:201:in `process_action'
activerecord (3.1.3) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.1.3) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.1.3) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.1.3) lib/action_controller/metal.rb:193:in `dispatch'
actionpack (3.1.3) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.1.3) lib/action_controller/metal.rb:236:in `block in action'
actionpack (3.1.3) lib/action_dispatch/routing/route_set.rb:65:in `call'
actionpack (3.1.3) lib/action_dispatch/routing/route_set.rb:65:in `dispatch'
actionpack (3.1.3) lib/action_dispatch/routing/route_set.rb:29:in `call'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:152:in `block in call'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:96:in `block in recognize'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:75:in `optimized_each'
rack-mount (0.8.3) lib/rack/mount/code_generation.rb:95:in `recognize'
rack-mount (0.8.3) lib/rack/mount/route_set.rb:141:in `call'
actionpack (3.1.3) lib/action_dispatch/routing/route_set.rb:532:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.3.6) lib/rack/etag.rb:23:in `call'
rack (1.3.6) lib/rack/conditionalget.rb:35:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/flash.rb:247:in `call'
rack (1.3.6) lib/rack/session/abstract/id.rb:195:in `context'
rack (1.3.6) lib/rack/session/abstract/id.rb:190:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/cookies.rb:331:in `call'
activerecord (3.1.3) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.1.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:477:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (3.1.3) lib/active_support/callbacks.rb:392:in `_run_call_callbacks'
activesupport (3.1.3) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.1.3) lib/action_dispatch/middleware/callbacks.rb:28:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/reloader.rb:68:in `call'
rack (1.3.6) lib/rack/sendfile.rb:101:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/show_exceptions.rb:47:in `call'
railties (3.1.3) lib/rails/rack/logger.rb:13:in `call'
rack (1.3.6) lib/rack/methodoverride.rb:24:in `call'
rack (1.3.6) lib/rack/runtime.rb:17:in `call'
activesupport (3.1.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.3.6) lib/rack/lock.rb:15:in `call'
actionpack (3.1.3) lib/action_dispatch/middleware/static.rb:53:in `call'
railties (3.1.3) lib/rails/engine.rb:456:in `call'
rack (1.3.6) lib/rack/content_length.rb:14:in `call'
rack (1.3.6) lib/rack/handler/webrick.rb:59:in `service'
/Users/jandrews/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Users/jandrews/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Users/jandrews/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
Request

Parameters:

{"authenticity_token"=>"tx+PRUMMQpyZ48Yw9ZLoWZ60HOo1992tpQXbKFkF4YM=",
 "product_id"=>"5"}
Show session dump

Show env dump

Response

Headers:

None

The code that actually does the submit looks like this in the browser

<form action="/line_items?product_id=5" class="button_to" method="post">
  <div>
    <input type="submit" value="Add to Cart">
    <input name="authenticity_token" type="hidden" 
      value="tx+PRUMMQpyZ48Yw9ZLoWZ60HOo1992tpQXbKFkF4YM=">
  </div>
</form>

Sorry forgot to mention if I add the lines one by one from the original it fails on the line with. That’s as far as I was able to debug since my knowledge of ruby is still limited atm.

@cart = current_cart

Cart model file.

class Cart < ActiveRecord::Base
  has_many :line_items, dependent: destroy
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-05-29T09:58:45+00:00Added an answer on May 29, 2026 at 9:58 am

    instead of

    has_many :line_items, dependent: destroy
    

    put

    has_many :line_items, dependent: :destroy # the colon!!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently reading a book, and learning ruby on rails. (Agile Web Development
I started reading Agile Web Development with Rails, its a very good book! I
I am reading Pragmatic Agile Web Development with Rails (2th ed) and trying examples
I'm reading the book Agile web developement with Rails 4Th editon, when he present
I'm currently reading Agile Web Development With Rails, 3rd edition. On page 672, I
I am learning Rails the age old way. By reading Agile Web Development with
I'm reading Agile web Development to learn Rails 3.0. The author is teaching us
I have been hearing and reading about Agile for years. I own a book
Hallo everybody Recently I've been reading the book: Agile software development, Principles, Patterns and
We're developing a data heavy modular web application stack with java but have little

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.