开发者

Rails customized "time_ago_in_words"-ish type model

开发者 https://www.devze.com 2023-02-28 19:50 出处:网络
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

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消