2013-06-29 95 views
1

我有一个视图文件(show.json.erb)显示我的@step模型属性,并且我想格式化DateTime属性“published_on”。以我目前的实现中,我得到的错误:为json格式化日期时间

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

我在我的模型解决了这个被定义的属性:

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