Ruby on Rails group_by(如何按月对事件进行分组) [英] Ruby on Rails group_by (how to group events by month)
问题描述
大家好,我是 Rails 新手.我有一个创建事件的控制器,我想按月(此类事件日期的月份)对它们进行分组.我正在使用 rails 4,这是我的代码.
这是我的控制器
def 索引@events = Event.all@event_months = @events.group_by { |t|t.due_at.beginning_of_month }结尾
这是我的视图代码
<% @event_months.each do |month, events|%><h2><%=month.strftime('%B')%></h2><% 事件中的事件%><a href="<%= event.url %>"><%= image_tag event.image_url(:normal), :class =>'eimage', :style =>'宽度:100%;'%><div class='event-content'><h1 class='event-title'><%= event.name %></h1><h2 class='event-place'><%= event.where %></h2><h3 class='event-time-date'><%= event.start_time.strftime("%B %d @ %I:%M %p") %></h3>
</a>
<%结束%><%= link_to 'Show',事件 %><%= link_to 'Edit', edit_event_path(event) %><%= link_to 'Destroy', event, method: :delete, data: { confirm: 'Are you sure?'}%><%结束%>
提前致谢.
def index@events = Event.all@event_months = @events.group_by { |t|t.due_at.month }结尾
视图中的月份名称由
给出日期::MONTHNAMES[月]
Hey Guys I am new to rails. I have a controller that create events and I want to group them by month (the month of such event date). I am using rails 4 here is my code.
This is my controller
def index
@events = Event.all
@event_months = @events.group_by { |t| t.due_at.beginning_of_month }
end
This is my view code
<% @event_months.each do |month, events| %>
<h2><%= month.strftime('%B') %></h2>
<% for event in evetns %>
<div class='event-card'>
<a href="<%= event.url %>">
<div class='event-image'>
<%= image_tag event.image_url(:normal), :class => 'eimage', :style => 'width:100%;' %>
</div>
<div class='event-content'>
<h1 class='event-title'><%= event.name %></h1>
<h2 class='event-place'><%= event.where %></h2>
<h3 class='event-time-date'><%= event.start_time.strftime("%B %d @ %I:%M %p") %></h3>
</div>
</a>
</div>
<% end %>
<%= link_to 'Show', event %>
<%= link_to 'Edit', edit_event_path(event) %>
<%= link_to 'Destroy', event, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
Thank in advance.
def index
@events = Event.all
@event_months = @events.group_by { |t| t.due_at.month }
end
Month Name in view is given by
Date::MONTHNAMES[month]
这篇关于Ruby on Rails group_by(如何按月对事件进行分组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!