ruby on rails - how to remove this error : undefined method `inc' for nil:NilClass -
i wrote in browser " http://localhost:3000/items/create?name=car1&description=good+car&price=500000&weight=0&real=1 " , , got error
undefined method `inc' nil:nilclass
extracted source (around line #11):
9 after_initialize { puts 'initialize' } 10 after_save { puts 'saved' } 11 after_create { category.inc(:items_count, 1) } 12 after_update { puts 'updated' } 13 after_destroy { category.inc(:items_count, -1) } 14
rails.root: e:/work/my_store
application trace | framework trace | full trace
app/models/item.rb:11:in `block in <class:item>' app/controllers/items_controller.rb:9:in `create'
request
parameters:
{"name"=>"car1", "description"=>"good car", "price"=>"500000", "weight"=>"0", "real"=>"1"}
in line 11 you're doing category.inc(:items_count, 1)
means category nil @ moment. don't see controller code, guess either did not create category yet, or did not set category
variable proper value. , there no need things
category.inc(:items_count, 1)
because if set properly, can do
category.items.count
to count items in category.
did try complete tutorial yet? recommend michael hartl's tutorial. it's simple, can learn quite complicated things it.