在Ruby on Rails中格式化时间和日期

时间:2015-10-31 19:36:25

标签: ruby-on-rails ruby

正常运行a:b:c:...:[]

rails c

但是运行2.2.3 :002 > Post.find(1).created_at.strftime("Posted on %B %d, %Y at %H:%M") Post Load (0.7ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT 1 [["id", 1]] => "Posted on October 23, 2015 at 15:31" 我收到错误:

rails server

index.html.erb看起来像:

index.html.erb where line #28 raised:
Object must be a Date, DateTime or Time object. "Posted on October 26, 2015 at 11:44" given.

1 个答案:

答案 0 :(得分:0)

看起来问题只是代码开头的一个迷路l

这应该有效:

<%= post.created_at.strftime("Posted on %B %d, %Y at %H:%M") %>

更好的是将此时格式化为模型并在帖子上调用。并在方法之外移动“发布”。所以:

<td> Posted on <%= post.formatted_created_on %> </td>