如何根据日期和/或时间渲染部分内容?
我正在 Rails3 中工作,我想根据特定的日期/时间渲染部分内容。例如,在圣诞节我想显示圣诞节内容,在万圣节显示万圣节内容等。如果将部分内容存储在某个数据存储中并根据某些元数据调用,那就太好了。
我可以以一种一一的方式做到这一点,但我想知道是否有人知道可能已经做了这样的事情的 Gem。我相当确定我不是第一个想要根据日期/时间呈现代码的人。但是,如果没有现成的解决方案,您是否有任何关于如何构建一种机制以允许“内容所有者”在没有程序员参与的情况下维护这些部分的线索?
I'm working in Rails3 and I would like to render a partial depending on a specific date/time. For example, on Christmas I want to show the Christmas content, on Halloween the Halloween content, etc. It would be great if the partials were stored in a datastore somewhere and called out depending on some metadata.
I can do this in a one-by-one type of way but I wanted to know if anyone knew of a Gem that may already do something like this. I'm fairly certain that I'm not the first person to want to render code depending on a date/time. However, if there are no ready made solutions, do you have any clues as to how to architect a mechanism to allow "content-owners" to maintain these partials without programmer involvement?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会有一个带有“date_start”和“content”字段的表。
在控制器中,我将选择“start_date”小于或等于当前日期的第一条记录,然后呈现该内容。请参阅下面的代码(未经测试,但您应该了解要点)。
在控制器中:
@record = Table.where('start_date <= curdate()'.order('start_date desc').limit(1)
在视图中
<%= RedCloth.new(@record.content).to_html。 html_safe %>
这应该可以帮助您开始,如果您需要更多信息,请告诉我。重构后,您可以将控制器中的代码放入模型中并使用范围。
I would have a table with a 'date_start' and 'content' fields.
In the controller, I would select the first record where 'start_date' is less than or equal to the current date and then render that content. See the code below (untested, but you should get the gist).
in controller:
@record = Table.where('start_date <= curdate()'.order('start_date desc').limit(1)
in view
<%= RedCloth.new(@record.content).to_html.html_safe %>
That should get you started, let me know if you need more information. Once you refactor, you could put the code from the controller in a model and use scopes.