Forum: Ruby on Rails After adding the item, an error undefined method `key?' for nil:NilClass

5352e063f844eed9d9c2777f20246e31?d=identicon&s=25 Dmitrij B. (dmitrij_b)
on 2013-03-18 08:46
Hey.

After adding the item, an error undefined method `key?' for nil:NilClass

its my controller products:

...
# GET /products/new
  # GET /products/new.json
  def new
    @product = Product.new

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

model product:

lass Product < ActiveRecord::Base
 has_many :line_items
 before_destroy : ensure_not_referenced_by_any_line_item

  attr_accessible :title, :description, :image_url, :price

  validates :title, :description, :image_url, :price,  presence: true
  validates :price,  numericality: {greater_than_or_equal_to: 0.01}
  validates :title, uniqueness: true
 # validates :image_url, allow_blank: true, format: {
   # with: %r{ \.(gif|jpg|png)$}i,
    #message: 'gif, jpg png. '
  #}

  def ensure_not_referenced_by_any_line_item
    if line_items.empty?
      return true
    else
      errors.add(:base, " существуют товарные позиции")
      return false
    end

end

class CreateProducts < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.string :title
      t.text :description
      t.string :image_url
      t.decimal :price

      t.timestamps
    end
  end
end

How solve this problem?
E1f877eccccc9e714b4d4fe03917f2a7?d=identicon&s=25 Barry (Guest)
on 2013-03-18 09:20
(Received via mailing list)
lass Product < ActiveRecord::Base

I don't know if this typo is just here and occured when you copied your
source code, or you have 'lass Product' in your model. If second, that
may
be reason for error

, 18  2013 ., 11:46:48 UTC+4  Ruby-Forum.com
User :
5352e063f844eed9d9c2777f20246e31?d=identicon&s=25 Dmitrij B. (dmitrij_b)
on 2013-03-18 09:26
no, its easy copy. class Product < ActiveRecord::Base
E1f877eccccc9e714b4d4fe03917f2a7?d=identicon&s=25 Barry (Guest)
on 2013-03-18 09:36
(Received via mailing list)
why do you show code of Product controller, if you create new Line_Item?

, 18  2013 ., 11:46:48 UTC+4  Ruby-Forum.com
User :
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2013-03-18 09:39
(Received via mailing list)
On 18 March 2013 07:46, Dmitrij B. <lists@ruby-forum.com> wrote:
> Hey.
>
> After adding the item, an error undefined method `key?' for nil:NilClass

That error means that something is nil when it should not be.
Unless I have missed it, you have not shown us which line is
generating the error.  If you cannot work it out from the stack trace
post the complete trace here.  If it refers to a line in your code
then tell us which that line is..

First, though, have a look at the Rails Guide on Debugging which will
show you techniques that you can use to debug the code yourself.  It
is always better to work a problem out for yourself if possible.

Colin
5352e063f844eed9d9c2777f20246e31?d=identicon&s=25 Dmitrij B. (dmitrij_b)
on 2013-03-18 09:40
no i want add  new product - http://localhost:3000/products/new

undefined method `key?' for nil:NilClass

Rails.root: /home/dima/RubyOnRails/Projects/depot

its trace - http://pastebin.com/upTRxFfK
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.