对数组进行排序和分组,最后显示所有项目的哈希值

时间:2014-12-05 22:23:39

标签: ruby-on-rails sorting grouping

我有一个包含传单数据的表,该数据按控制器中的日期排序:

@leaflets = Leaflet.order(:date)

然后按视图中的日期分组(我在互联网上找到了代码)

<%= @leaflets.group_by(&:date).each do |date, leaflets| %>
  <p><%= l(date, :format => '%A, %d %b %Y') %></p> <!-- localized date formatting: weekday, day month year -->


  <table>
    <tr>
      <% leaflets.each do |leaflet| %>

        <td style="border:dotted 1px; padding:5px;">

      <strong><%= leaflet.place %></strong><br/>
      <%= leaflet.description %><br/>
      <%= link_to "Zapisz się", leaflet_path(leaflet) %>
      </td>
      </a>
    <% end %>
    </tr>

  </table>

  <% end %>

一切都很好,但它最后将所有项目打印为哈希:

{Tue, 11 Nov 2014=>[#<Leaflet id: 14, date: "2014-11-11", place: "Miejsce11-11-A", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:58:01", updated_at: "2014-12-05 21:58:01">, #<Leaflet id: 15, date: "2014-11-11", place: "Miejsce11-11-B", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:58:04", updated_at: "2014-12-05 21:58:04">, #<Leaflet id: 16, date: "2014-11-11", place: "Miejsce11-11-C", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:58:07", updated_at: "2014-12-05 21:58:07">], Mon, 01 Dec 2014=>[#<Leaflet id: 4, date: "2014-12-01", place: "Miejsce1", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:56:42", updated_at: "2014-12-05 21:56:42">, #<Leaflet id: 5, date: "2014-12-01", place: "Miejsce2", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:56:55", updated_at: "2014-12-05 21:56:55">, #<Leaflet id: 6, date: "2014-12-01", place: "Miejsce3", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:57:01", updated_at: "2014-12-05 21:57:01">], Tue, 02 Dec 2014=>[#<Leaflet id: 7, date: "2014-12-02", place: "Miejsce02-A", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:57:21", updated_at: "2014-12-05 21:57:21">, #<Leaflet id: 8, date: "2014-12-02", place: "Miejsce02-B", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:57:24", updated_at: "2014-12-05 21:57:24">, #<Leaflet id: 9, date: "2014-12-02", place: "Miejsce02-C", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:57:28", updated_at: "2014-12-05 21:57:28">], Fri, 05 Dec 2014=>[#<Leaflet id: 10, date: "2014-12-05", place: "Miejsce05-A", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:57:40", updated_at: "2014-12-05 21:57:40">, #<Leaflet id: 11, date: "2014-12-05", place: "Miejsce05-B", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:57:43", updated_at: "2014-12-05 21:57:43">, #<Leaflet id: 12, date: "2014-12-05", place: "Miejsce05-C", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:57:46", updated_at: "2014-12-05 21:57:46">, #<Leaflet id: 13, date: "2014-12-05", place: "Miejsce05-D", description: nil, program: nil, modifier: nil, created_at: "2014-12-05 21:57:48", updated_at: "2014-12-05 21:57:48">]}

知道什么是错的吗?在Rails中进行分组是正常的行为吗?如何删除这个大哈希? 屏幕截图:http://imgur.com/Nc9sYS7

2 个答案:

答案 0 :(得分:0)

只需删除=

<%= @leaflets.group_by(&:date).each do |date, leaflets| %>

所以它写着:

<% @leaflets.group_by(&:date).each do |date, leaflets| %>

=正在输出each来电的结果。

答案 1 :(得分:0)

each方法返回您调用的对象each。在ERB中,<%= ... %>显示已评估代码的结果,在这种情况下为@leaflets.group_by(&:date)。只需删除=符号即可静默评估代码:

<% @leaflets.group_by(&:date).each do |date, leaflets| %>
    ...
<% end %>