Dmitrij B.
2013-Mar-18 07:46 UTC
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Barry
2013-Mar-18 08:19 UTC
Re: After adding the item, an error undefined method `key?'' for nil:NilClass
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 написал:> > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/TF6bS_oD7tYJ. For more options, visit https://groups.google.com/groups/opt_out.
Dmitrij B.
2013-Mar-18 08:26 UTC
Re: After adding the item, an error undefined method `key?'' for nil:NilClass
no, its easy copy. class Product < ActiveRecord::Base -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Barry
2013-Mar-18 08:35 UTC
Re: After adding the item, an error undefined method `key?'' for nil:NilClass
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 написал:> > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/XBARlQ0seecJ. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Mar-18 08:38 UTC
Re: After adding the item, an error undefined method `key?'' for nil:NilClass
On 18 March 2013 07:46, Dmitrij B. <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hey. > > After adding the item, an error undefined method `key?'' for nil:NilClassThat 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 -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Dmitrij B.
2013-Mar-18 08:40 UTC
Re: After adding the item, an error undefined method `key?'' for nil:NilClass
no i want add new product - http://localhost:3000/products/new -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.