0
我将尖叫中的所有信息作为JSON返回。以JSON方式返回时间差
我想返回它创建多久以前。
include ActionView::Helpers::DateHelper
def as_json(options={})
super(:only => [:id, :yell_type, :status, :payment_type],
:include => {
:trade_offer => {:only => [:id, :title, :description, :price],
:include => [:photos => {:only => [:id, :url]}]
},
:categories => {:only => [:id, :name]},
:user => {:only => [:id, :name, :avatar]}
},
:methods => [ times_ago(:create_at) ]
)
end
def times_ago(create_at)
time_ago_in_words(create_at)
end
这将返回一个错误:
comparison of Symbol with Time failed
我应该怎么办呢?
男人是可能的我通过参数日期creação的方法? –
不是对象的'created_at'部分吗?如果是这样,你可以有'times_ago'方法,你可以返回'self.created_at'。否则,如果'created_at'不是对象的一部分,则可以在调用'obj.to_json'之前将该值设置为该对象,然后在'#times_ago'中将此值设置为'self.created_at' –