Rails 定制的“time_ago_in_words”类型模型

发布于 2024-11-02 17:46:12 字数 437 浏览 8 评论 0原文

大家早上好!

我正在开发一个 Rails 应用程序,只需简单检查是或否,然后它会显示 created_at 行以及选中的框。我的问题是我需要将 created_at 显示为不同的值,而不是时间。如果有人在凌晨 4:00 至上午 10:00 之间创建了一个条目,则会在 created_at 字段的视图中显示早餐。如果他们在上午 10:00 到下午 2:00 之间创建它,它将显示午餐
我在 Google 上搜索了几个小时,我能想到的最接近的东西是 time_ago_in_words 的一些变体,但没有任何效果。
我将如何实施这种性质的事情?

全面披露:我对 Ruby/RoR 有点业余。温柔一点...

Morning, everyone!

I'm working on a Rails app that is just a simple check yes or no, then it displays the created_at row and whichever box was checked. My issue is that I need to display the created_at as a different value, not the time. If someone created an entry between 4:00 AM - 10:00 AM, it would display Breakfast in the view for the created_at field. If they created it between 10:00 AM and 2:00 PM, it would display Lunch.
I've Googled for hours, and the closest thing I could come up with is some variation of time_ago_in_words but nothing really works.
How would I go about implementing something of this nature?

Full disclosure: I'm a bit of an amateur at Ruby/RoR. Be gentle...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅笑依然 2024-11-09 17:46:12

您可以创建自己的

case created_at.hour
when (4..10)
  "Breakfast"
when (10..14)
  "Lunch"
when (14..22)
  "Supper"
else 
  "Night"
end

您也可以将其包装为方法或用它扩展 DateTime

You can create your own

case created_at.hour
when (4..10)
  "Breakfast"
when (10..14)
  "Lunch"
when (14..22)
  "Supper"
else 
  "Night"
end

You can also wrap it as a method or extend DateTime class with it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文