我有一个显示我的@step模型属性的视图文件(show.json.erb),我想格式化DateTime属性" published_on"。在我当前的实现中,我收到错误:
undefined method `strftime' for :published_on:Symbol
这是我的代码:
{
"success":true,
"info":"ok",
"data":
{ "step":
<%= JSON.pretty_generate(@step.as_json(only: [:name, :description, :id, :last, :position, :published_on.strftime("%m/%d/%Y %H:%M:%S")], include: {images: {only: [:file, :position] } } )).html_safe %>
}
}
有关如何解决此错误的任何想法?
答案 0 :(得分:0)
我通过在我的模型中定义了一个属性来解决这个问题:
def formatted_date
published_on.strftime("%m/%d/%Y %H:%M:%S")
end
然后我在我的json文件中引用它:
<%= JSON.pretty_generate(@step.as_json(only: [:name, :description, :id, :last], methods: :formatted_date, include: {images: {only: [:file, :position] } } )).html_safe %>