Monday, March 18, 2013

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

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?

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate