代码:
#schema..
t.integer "shares", :default => 0
end
#模型
def increment_shares
shares = shares + 1 #undefined method + for nil class
save
end
#console
irb(main):003:0> t.shares
=> 0
irb(main):004:0> t.shares = t.shares + 1
=> 1
irb(main):005:0> t.increment_shares
NoMethodError: undefined method `+' for nil:NilClass
(问题应该从代码中清楚。编写更多文本以满足“质量标准”)