<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Pragmatic Forums | Posts in topic 'NoMethodError in LineItemsController#create'</title>
    <link>/forums/148/topics/10289.rss</link>
    <language>en-us</language>
    <ttl>60</ttl>
    <item>
      <title>NoMethodError in LineItemsController#create posted by djfox @ Thu, 02 Feb 2012 18:33:29 +0000</title>
      <description>&lt;p&gt;Sam help me on this topic please! &lt;a href="http://forums.pragprog.com/forums/148/topics/9635"&gt;http://forums.pragprog.com/forums/148/topics/9635&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 02 Feb 2012 18:33:29 +0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:148:10289:28632</guid>
      <author>djfox</author>
      <link>http://forums.pragprog.com/forums/148/topics/10289</link>
    </item>
    <item>
      <title>NoMethodError in LineItemsController#create posted by djfox @ Thu, 02 Feb 2012 18:32:00 +0000</title>
      <description>&lt;p&gt;Solves the problem! I think (I&amp;#8217;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?&lt;/p&gt;</description>
      <pubDate>Thu, 02 Feb 2012 18:32:00 +0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:148:10289:28631</guid>
      <author>djfox</author>
      <link>http://forums.pragprog.com/forums/148/topics/10289</link>
    </item>
    <item>
      <title>NoMethodError in LineItemsController#create posted by Sam Ruby @ Thu, 02 Feb 2012 18:00:16 +0000</title>
      <description>&lt;p&gt;current_item.quantity must be nil.  Did you set the default when you migrated?&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;add_column :line_items, :quantity, :integer, default: 1&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;If not, try reverting the migration, updating it, and then reapplying the migration.&lt;/p&gt;</description>
      <pubDate>Thu, 02 Feb 2012 18:00:16 +0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:148:10289:28630</guid>
      <author>Sam Ruby</author>
      <link>http://forums.pragprog.com/forums/148/topics/10289</link>
    </item>
    <item>
      <title>NoMethodError in LineItemsController#create posted by djfox @ Tue, 31 Jan 2012 22:10:35 +0000</title>
      <description>&lt;p&gt;hello my problem is that it shows the value &amp;#8220;item.quantity&amp;#8221; when I click on &amp;#8220;add button&amp;#8221; after the first time and I get the error that I mentioned. The full error I have is:&lt;/p&gt;


	&lt;p&gt;&lt;code&gt;NoMethodError in LineItemsController#create&lt;/code&gt;&lt;/p&gt;


	&lt;p&gt;You have a nil object when you didn&amp;#8217;t expect it!&lt;br /&gt;You might have expected an instance of Array.&lt;br /&gt;The error occurred while evaluating nil.+&lt;/p&gt;


	&lt;p&gt;app/models/cart.rb:7:in `add_product&amp;#8217;&lt;br /&gt;app/controllers/line_items_controller.rb:45:in `create&amp;#8217;&lt;/p&gt;


	&lt;p&gt;The classes are as follows:&lt;/p&gt;


	&lt;p&gt;&lt;code&gt;application_controller.rb&lt;/code&gt; :&lt;br /&gt;&lt;pre&gt;
&lt;code&gt;
class ApplicationController &amp;lt; 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
&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;&lt;code&gt;carts_controller.rb&lt;/code&gt; :&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
class CartsController &amp;lt; 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
&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;

	&lt;p&gt;&lt;code&gt;line_items.rb&lt;/code&gt; :&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
class LineItemsController &amp;lt; 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
&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;

	&lt;p&gt;&lt;code&gt;Models&lt;/code&gt; :&lt;/p&gt;


	&lt;p&gt;&lt;code&gt;cart.rb&lt;/code&gt; :&lt;br /&gt;&lt;pre&gt;
&lt;code&gt;
class Cart &amp;lt; ActiveRecord::Base
  has_many :line_items, :dependent=&amp;gt; :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
&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;&lt;code&gt;line_items&lt;/code&gt; :&lt;br /&gt;&lt;pre&gt;
&lt;code&gt;
class LineItem &amp;lt; ActiveRecord::Base
  belongs_to :product
  belongs_to :cart
end
&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;&lt;code&gt;Views&lt;/code&gt; :&lt;/p&gt;


	&lt;p&gt;in the views I think the problem starts in the view carts &amp;#8220;show&amp;#8221; where &lt;code&gt; "item.quantity" &lt;/code&gt; does not show:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
&amp;lt;% if notice %&amp;gt;
&amp;lt;p id="notice" &amp;gt;&amp;lt;%= notice %&amp;gt;&amp;lt;/p&amp;gt;
&amp;lt;% end %&amp;gt;

&amp;lt;h2&amp;gt;Your Pragmatic Cart&amp;lt;/h2&amp;gt;
&amp;lt;ul&amp;gt;
&amp;lt;% @cart.line_items.each do |item| %&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;%= item.quantity %&amp;gt; &amp;amp;times; &amp;lt;%= item.product.title %&amp;gt; &amp;lt;/li&amp;gt;
&amp;lt;% end %&amp;gt;
&amp;lt;/ul&amp;gt;
&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;

	&lt;p&gt;please help me!!&lt;/p&gt;</description>
      <pubDate>Tue, 31 Jan 2012 22:37:13 +0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:148:10289:28588</guid>
      <author>djfox</author>
      <link>http://forums.pragprog.com/forums/148/topics/10289</link>
    </item>
  </channel>
</rss>
