我有一个带有下一个字段的对象:
class Post
field :repost , type: Integer
attr_accessible :repost
end
在我重新发布 Action 的 Controller 中,我想增加计数器 + 1。
我尝试:
@post.repost += 1
但我得到:
NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
我该如何解决?
最佳答案
首先使用默认零初始化repost
field :repost , type: Integer, :default => 0
@post.repost += 1
关于ruby-on-rails - 对象属性的 ruby 计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9160333/