Ruby on rails 格式化json的日期时间

Ruby on rails 格式化json的日期时间,ruby-on-rails,json,datetime,Ruby On Rails,Json,Datetime,我有一个显示@step模型属性的视图文件(show.json.erb),我想格式化DateTime属性“published_on”。在我当前的实现中,我得到了以下错误: undefined method `strftime' for :published_on:Symbol 这是我的密码: { "success":true, "info":"ok", "data": { "step": <%= JSON.pretty_

我有一个显示@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 %>
    } 
}
{
“成功”:没错,
“信息”:“确定”,
“数据”:
{“步骤”:
} 
}

关于如何修复此错误,有什么想法吗?

我通过在模型中定义一个属性来解决此问题:

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 %>