ERB 中包含工作日名称的表格

发布于 2024-12-13 21:21:20 字数 561 浏览 2 评论 0原文

我对 Ruby/Rails 非常陌生,我只是想找到在 ERB 中创建表格的最优雅的方法,其中列是工作日,行是给定模型的每条记录。这个概念本身很简单,我可能可以通过一堆手动代码来完成它,但我想尽可能多地利用rails/erb魔法。更具体地说,这是我的定义。

  • 列出所有用户的模型 列出
  • 给定日期每个用户的单一状态的模型(每个用户在给定日期只能有一个状态:说“开”或“关”)

我想要一个列出所有用户的视图表格中的用户及其该周的状态。例如:

       Mon | Tue | ... | Fri
User1   on | off | ... |
User2  off |     | ... |

有几点需要注意: -(正如我已经提到的)一个用户每天只能有一个状态 - 用户在某一天可能没有任何状态 - 周六/周日没有栏目 - 该表将始终显示本周的完整情况,即使这一天是星期二(即在这种情况下,星期三 .. 星期五条目将为空白)。

现在,我再次确信我可以编写一堆 erb 代码来手动完成这一切,但希望使用一些 Rails 魔法。任何指示或帮助将不胜感激。

I am very new to Ruby/Rails and just I am trying to find the most elegant way to do a table in ERB where the columns are the weekday and the rows are each record for a given model. The concept itself is easy and I can probably do it through a bunch of manual code, but I would like to leverage as much rails/erb magic as possible. To be more specific, here is what I have defined.

  • A model that lists all users
  • A model that lists a single status for each user for a given date (each user can only have one status for a given date: say 'on' or 'off')

I want a view that lists all the users in a table with their status for that week. Eg:

       Mon | Tue | ... | Fri
User1   on | off | ... |
User2  off |     | ... |

A couple things to note:
- (as I already mentioned) A user can only have one status per day
- A user may not have any status for a given day
- No columns for Sat/Sun
- The table will always show the full current week, even if the day is on Tue (ie. in that case the Wed .. Fri entries will just be blank).

Now, again, I am sure I can write a bunch of erb code to do this all manually, but was hoping to use some rails magic. Any pointers or help would greatly be appreciated.

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

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

发布评论

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

评论(1

鹤舞 2024-12-20 21:21:20

您始终可以执行以下操作:

@monday = Date.today.at_beginning_of_week
7.times do |x|
  @days << x.days.since(@monday)
end

然后在您的视图中循环@days。

您可以打印日期,或仅打印小时表行中的日期。有用之处在于您还可以使用该日期查找用户状态。

You could always do something like:

@monday = Date.today.at_beginning_of_week
7.times do |x|
  @days << x.days.since(@monday)
end

Then loop through @days in your view.

You could print the date, or just the day in hour table rows. Useful in that you can also look up your users status using that date.

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