上图就是我的意思。
我认为代码应该是这样的:
<% @days_from_this_week.each do |day| %>
<%= day.name %>
<!--e.g. Monday-->
<% day.sessions do |session| %>
<% @session_categories.each do |category| %>
<!--categories means the Sesion1/2/3/4 category
A day's sessionX could be empty, so we have to determine wheather a session matches its cateogry,
and then display it in the table
-->
<% if session.category == category %>
<%= session.content %>
<% end %>
<% end %>
<% end %>
<% end %>
但是如何获得
current week
?以及如何获得name of a day
?并浏览此日历,pre_week
和 next_week
也是需要的。我找到了
prev_week
和 next_week
方法,但似乎一周后返回某一天,这不是我需要的。一个可能的解决方案是 here ,但我不知道如何使用它。
更新:
我找到了一个可行的解决方案,但仍在寻找更好的代码:
<!-- @date = params[:date] ? Date.parse(params[:date]) : Date.today
first=@date.at_beginning_of_week
last=@date.at_beginning_of_week + 6
@days= (first..last).to_a-->
<% @days.each do |day| %>
<div>
<%= day %>
</div>
<% end %>
最佳答案
在您设置 @days_from_this_week
的 Controller 中试试这个
today = Date.today # Today's date
@days_from_this_week = (today.at_beginning_of_week..today.at_end_of_week).map
要显示它的作用:
> (today.at_beginning_of_week..today.at_end_of_week).map.each { |day| day }
> [Mon, 12 Aug 2013, Tue, 13 Aug 2013, Wed, 14 Aug 2013, Thu, 15 Aug 2013, Fri, 16 Aug 2013, Sat, 17 Aug 2013, Sun, 18 Aug 2013]
更新查询文章的附加请求,发布于:
查找从开始日期的午夜到第二天之间的所有文章。
start_time = Time.parse(date.to_s)
end_time = 1.day.since(start_time)
Article.where('published_on >= ? and published_on < ?', start_time, end_time)
关于ruby-on-rails - Ruby on Rails : How to get the current week and loop through it to display its days?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18199705/