如何在模型中显示按日期分隔的表格

发布于 2024-12-22 15:51:18 字数 642 浏览 3 评论 0原文

使用 Enumerable,我尝试按日期在有组织的事件日历中显示日期。不是标准的 7 格日历。

尝试获取:

2011 年 12 月 1 日 活动1 活动2 活动 3

2011 年 12 月 2 日 活动4 活动 5

2011 年 12 月 3 日 事件 6

...

目前 Enumerable 让我很困惑。感谢您的任何指导

- 编辑 2011 年 12 月 22 日

当前数据:

控制器字符串 -

@events = Event.where('start >= ?', Date.today).paginate(:per_page => 15, :page => params[:page])

视图调用 -

<% @events.each do |event| %>
  <table stuff here>
<% end %>

方法 - 到目前为止尚未指定任何可枚举的方法 -

我的表包括以下字段; :title、:venue、:address(已地理编码)、:about(文本)、:start(日期时间函数)

Using Enumerable, I am trying to display dates in an organized event calendar by dates. Not as a standard 7 grid calendar.

Trying to get:

December 1, 2011
Event 1
Event 2
Event 3

December 2, 2011
Event 4
Event 5

December 3, 2011
Event 6

...

Currently Enumerable confuses the hell outta me. Thanks for any guidance--

EDIT Dec.22,2011

Current Data:

Controller string-

@events = Event.where('start >= ?', Date.today).paginate(:per_page => 15, :page => params[:page])

View call-

<% @events.each do |event| %>
  <table stuff here>
<% end %>

Method- Have not specified anything enumerable on method as of now---

My table includes the following fields; :title, :venue, :address (is geocoded), :about (text), :start (datetime function)

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

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

发布评论

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

评论(1

执笔绘流年 2024-12-29 15:51:18

更新问题更新:因此,您在 @events 中有一堆事件对象,因此您可以使用 group_by

@events_by_date = @events.group_by { |e| e.start.to_date }

这将为您提供 @events_by_date 中带有日期的哈希值作为钥匙和Event 对象的数组作为值。

然后,您需要一个日期数组的数组来匹配您的月份并从该数组驱动表:

<table>
    <% @month.each do |week| %>
        <tr>
            <% week.each do |day| %>
                <td>
                    <% if day %>
                        <%= day.day %>
                        <% @events_by_date[day].to_a.each do |e| %>
                            <%= e.about %>
                        <% end %>
                    <% end %>
                </td>
            <% end %>
        </tr>
    <% end %>
</table>

当然会涉及一些格式,但结构是相似的。

Update for question update: So you have a bunch of event objects in @events so you could use a group_by:

@events_by_date = @events.group_by { |e| e.start.to_date }

That would give you a Hash in @events_by_date with dates as the keys and arrays of Event objects as the values.

Then you'd want an array of arrays of Dates to match your month and drive the table from that array:

<table>
    <% @month.each do |week| %>
        <tr>
            <% week.each do |day| %>
                <td>
                    <% if day %>
                        <%= day.day %>
                        <% @events_by_date[day].to_a.each do |e| %>
                            <%= e.about %>
                        <% end %>
                    <% end %>
                </td>
            <% end %>
        </tr>
    <% end %>
</table>

There would be some formatting involved of course but the structure would be similar.

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