0

I am trying to add items to a cart. I keep getting

Undefined local variable or method document_id' for #<Cart:0x8fc0130> Rails.root: C:/Users/cmendla/RubymineProjects/technical_library app/models/cart.rb:22:inadd_item' app/controllers/carts_controller.rb:7:in `add_to_cart' Request Parameters: {"_method"=>"post",

cart.rb
has_many :items
  def add_item(product_id)
    $test1 = 'add item 2'
    item = items.where('document_id = ?', document_id).first
    if item
      increase the quantity of product in cart
     item.quantity + 1
      save
    else
      product does not exist in cart
      product = Product.find(product_id)
      items << product
           document = Document.find(document_id)
           items << document

      cart.items << Item.new(document_id: document_id)

    end
    save
  end

Application controller:

application_controller.rb
  def current_cart
    if session[:cart_id]
      @current_cart ||= Cart.find(session[:cart_id])
    end
    if session[:cart_id].nil?
      @current_cart = Cart.create!
      session[:cart_id] = @current_cart.id
    end
    @current_cart
  end

.

class Item < ActiveRecord::Base
  belongs_to :cart
end

carts controller:

  def add_to_cart
    # @cart = current_cart
    current_cart.add_item(params[:document_id])
    redirect_to carts_path(current_cart.id)
    # redirect to shopping cart or whereever
  end

Routes.rb

   post '/add_to_cart/:doc_id' => 'carts#add_to_cart', :as => 'add_to_cart'

Schema:

 create_table "items", force: :cascade do |t|
    t.integer  "document_id"
    t.string   "user"
    t.integer  "cart_id"
    t.datetime "created_at",  null: false
    t.datetime "updated_at",  null: false
  end

  add_index "items", ["cart_id"], name: "index_items_on_cart_id"


create_table "carts", force: :cascade do |t|
    t.string   "user"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

I am calling the add action with the following link in my documents.index

<td><%= link_to "add",  add_to_cart_path(document.id), :method => :post %></td> 
3
  • you highlight line 22. the offending line? We lost that information here. Commented Jan 8, 2016 at 18:50
  • 2
    There is missing document_id parameter in the add_item method. Also you passing in the controller params[:document_id] as product_id parameter of this method. Commented Jan 8, 2016 at 18:56
  • You're passing 'document.id' to the 'add-item' method, which then assigns it to 'product_id'. Then, in that 'add-item' method, you try to look up the item by the variable 'document_id', which doesn't exist in the 'add-item' method, because you haven't defined it and assigned it a value. Commented Jan 8, 2016 at 18:57

1 Answer 1

1
item = items.where('document_id = ?', document_id).first

On that line document_id is not defined. should that actually be product_id?

Sign up to request clarification or add additional context in comments.

3 Comments

It should be document_id. I got sloppy transposing some code that was using product_id..
Well then you need to define document_id. perhaps document_id = params[:document_id]?
That definitely got me on the right track. I have to get the exact syntax now. For example, if I'm writing cart.item.document_id << Item.new(document_id: document_id) I need to to define cart and item with something like:.. ie ` item = Item.first` and cart = Cart.first . Of course, the definition will be more like the params you outlined above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.