Article.rb < ActiveRecord::Base
...
attr_reader :title
def title
self.title.gsub(/"/," ")
end
end我试图改写每一篇文章标题的显示方式,因为如果我不这样做,它看起来很难看,但是我一直收到这样的错误:
SystemStackError in ArticlesController#index or StackLevelTooDeep我不知道如何解决这个问题。如果我将方法更改为与ntitle不同的方法,它将起作用。为什么?!
发布于 2014-12-09 06:27:13
当您在self.title内部调用def title时,它会调用它自己,因此得到无限的递归,并导致错误StackLevelTooDeep。
这应该是可行的:
class Article < ActiveRecord::Base
...
def title
read_attribute(:title).to_s.gsub(?", " ")
end
endhttps://stackoverflow.com/questions/27370089
复制相似问题