small medium large xlarge

Generic-user-small
31 Jan 2012, 22:37
djfox (9 posts)

hello my problem is that it shows the value “item.quantity” when I click on “add button” after the first time and I get the error that I mentioned. The full error I have is:

@NoMethodError in LineItemsController#create@

You have a nil object when you didn’t expect it! You might have expected an instance of Array. The error occurred while evaluating nil.+

app/models/cart.rb:7:in add_product' app/controllers/line_items_controller.rb:45:in create’

The classes are as follows:

@application_controller.rb@ :

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

@carts_controller.rb@ :

class CartsController < ApplicationController
  # GET /carts
  # GET /carts.json
  def index
    @carts = Cart.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @carts }
    end
  end

  # GET /carts/1
  # GET /carts/1.json
  def show
    @cart = Cart.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @cart }
    end
  end

  # GET /carts/new
  # GET /carts/new.json
  def new
    @cart = Cart.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @cart }
    end
  end

  # GET /carts/1/edit
  def edit
    @cart = Cart.find(params[:id])
  end

  # POST /carts
  # POST /carts.json  
  def create  
    @cart = Cart.new(params[:cart])

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

  # PUT /carts/1
  # PUT /carts/1.json
  def update
    @cart = Cart.find(params[:id])

    respond_to do |format|
      if @cart.update_attributes(params[:cart])
        format.html { redirect_to @cart, notice: 'Cart was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @cart.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /carts/1
  # DELETE /carts/1.json
  def destroy
    @cart = Cart.find(params[:id])
    @cart.destroy

    respond_to do |format|
      format.html { redirect_to carts_url }
      format.json { head :ok }
    end
  end
end

@line_items.rb@ :

class LineItemsController < ApplicationController
  # GET /line_items
  # GET /line_items.json
  def index
    @line_items = LineItem.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @line_items }
    end
  end

  # GET /line_items/1
  # GET /line_items/1.json
  def show
    @line_item = LineItem.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @line_item }
    end
  end

  # GET /line_items/new
  # GET /line_items/new.json
  def new
    @line_item = LineItem.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @line_item }
    end
  end

  # GET /line_items/1/edit
  def edit
    @line_item = LineItem.find(params[:id])
  end

  # POST /line_items
  # POST /line_items.json  
  def create
    @cart = current_cart
    product = Product.find(params[:product_id])
    @line_item = @cart.add_product(product.id) #line 45
    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

  # PUT /line_items/1
  # PUT /line_items/1.json
  def update
    @line_item = LineItem.find(params[:id])

    respond_to do |format|
      if @line_item.update_attributes(params[:line_item])
        format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /line_items/1
  # DELETE /line_items/1.json
  def destroy
    @line_item = LineItem.find(params[:id])
    @line_item.destroy

    respond_to do |format|
      format.html { redirect_to line_items_url }
      format.json { head :ok }
    end
  end
end

@Models@ :

@cart.rb@ :

class Cart < ActiveRecord::Base
  has_many :line_items, :dependent=> :destroy
  
  def add_product(product_id)
    current_item = line_items.find_by_product_id(product_id)
    if current_item
     current_item.quantity += 1 #line 7
    else
      current_item = line_items.build(product_id: product_id)
    end
     current_item
  end
end

@line_items@ :

class LineItem < ActiveRecord::Base
  belongs_to :product
  belongs_to :cart
end

@Views@ :

in the views I think the problem starts in the view carts “show” where @ “item.quantity” @ does not show:

<% if notice %>
<p id="notice" ><%= notice %></p>
<% end %>
    
<h2>Your Pragmatic Cart</h2>
<ul>
<% @cart.line_items.each do |item| %>
<li><%= item.quantity %> &times; <%= item.product.title %> </li>
<% end %>
</ul>

please help me!!

Samr_small_pragsmall
02 Feb 2012, 18:00
Sam Ruby (634 posts)

current_item.quantity must be nil. Did you set the default when you migrated?

add_column :line_items, :quantity, :integer, default: 1

If not, try reverting the migration, updating it, and then reapplying the migration.

Generic-user-small
02 Feb 2012, 18:32
djfox (9 posts)

Solves the problem! I think (I’m new to Ruby) that creating a Scaffolfd with the lowercase name was where I generated the problem .. I returned to start all over and begging to be solved and so was! .. Now I wonder: Was it that the problem?

Generic-user-small
02 Feb 2012, 18:33
djfox (9 posts)

Sam help me on this topic please! http://forums.pragprog.com/forums/148/topics/9635

You must be logged in to comment