@activities 数据结构的通知提要问题,不确定如何遍历它

Notification feed issue with data structure of @activities, not sure how to loop through it

我正在尝试呈现我的通知提要活动,但我似乎无法弄清楚如何正确循环 @activities

对于背景,我遵循了推荐的 Sitepoint 教程:https://www.sitepoint.com/super-easy-activity-feeds-with-stream/

我的代码和那个基本一样。这是 @activities 对象:

[2] pry(#<FeedsController>)> @activities
=> [{"activities"=>
   [{"actor"=>
      #<User id: 1, email: "andy@dev.to", created_at: "2017-06-08-21:33:00", updated_at: "2017-06-13 18:15:59", name: "Andy">,
     "foreign_id"=>"Follow:4",
     "id"=>"861b2600-5063-11e7-8080-8000262131d5",
     "object"=>
      #<User id: 1, email: "andy@dev.to", created_at: "2017-06-08 21:33:00", updated_at: "2017-06-13 18:15:59", name: "Andy">,
     "origin"=>nil,
     "target"=>nil,
     "time"=>"2017-06-13T18:10:04.000000",
     "to"=>["notification:1"],
     "verb"=>"Follow"}],
  "activity_count"=>1,
  "actor_count"=>1,
  "created_at"=>"2017-06-13T18:10:04.000000",
  "group"=>"48946_2017-06-13",
  "id"=>"861b2600-5063-11e7-8080-8000262131d5",
  "is_read"=>false,
  "is_seen"=>false,
  "updated_at"=>"2017-06-13T18:10:04.000000",
  "verb"=>"Follow"},
 {"activities"=>
   [{"actor"=>
      #<User id: 1, email: "andy@dev.to", created_at: "2017-06-08 21:33:00", updated_at: "2017-06-13 18:15:59", name: "Andy">,
     "foreign_id"=>"Follow:1",
     "id"=>"8b249a80-4fbb-11e7-8080-80002a2ebc3f",
     "object"=>
      #<User id: 1, email: "andy@dev.to", created_at: "2017-06-08 21:33:00", updated_at: "2017-06-13 18:15:59", name: "Andy">,
     "origin"=>nil,
     "target"=>nil,
     "time"=>"2017-06-12T22:07:37.000000",
     "to"=>["notification:1"],
     "verb"=>"Follow"},
    {"actor"=>
      #<User id: 2, email: "niko@dev.to", created_at: "2017-06-08 21:35:33", updated_at: "2017-06-08 22:01:50", name: "Niko">,
     "foreign_id"=>"Follow:2",
     "id"=>"40edea00-4fb8-11e7-8080-8000106d4b0d",
     "object"=>
      #<User id: 1, email: "andy@dev.to", created_at: "2017-06-08 21:33:00", updated_at: "2017-06-13 18:15:59", name: "Andy">,
     "origin"=>nil,
     "target"=>nil,
     "time"=>"2017-06-12T21:44:04.000000",
     "to"=>["notification:1"],
     "verb"=>"Follow"}],
  "activity_count"=>2,
  "actor_count"=>2,
  "created_at"=>"2017-06-12T21:44:04.000000",
  "group"=>"48946_2017-06-12",
  "id"=>"8b249a80-4fbb-11e7-8080-80002a2ebc3f",
  "is_read"=>false,
  "is_seen"=>false,
  "updated_at"=>"2017-06-12T22:07:37.000000",
  "verb"=>"Follow"}]

这是主视图 (view/feeds/notification.html.erb):

<div class="page-header"><h1>Your notification feed</h1></div>

<% for activity in @activities %>

  <%= render_activity activity %>
<% end %>

我的部分 (views/aggregated_activity/_follow.html.erb):

<div class="well well-sm">
  <p><small class="text-muted"><%= time_ago_in_words activity['time'] -%> ago</small></p>

  <p><strong><%= activity['object'].name %></strong> and <strong><%= activity['actor'].name %></strong> are now friends</p>
</div>

如果我将上面的部分更改为以下内容,我可以访问正确的对象:

<div class="well well-sm">
  <p><small class="text-muted"><%= time_ago_in_words activity['activities'][0]['time'] %> ago</small></p>

  <p><strong><%= activity['activities'][0]['object'].name %></strong> and <strong><%= activity['activities'][0]['actor'].name %></strong> are now friends</p>
</div>

然而,这似乎不是解决问题的正确方法。似乎唯一显示的活动是我关注自己的情况,而不是我关注第二个用户 (John) 的情况。循环到这个散列数组的最佳方法是什么,它的值是数组?

我必须仔细检查 sitepoint.com 教程的最新情况,因为我们 (Stream) 没有编写它。

不过,回到您的问题:如您所见,我们的通知提要最终以不同的方式分组,您需要一个通知提要的双循环:

<% for group in activities %>
  <% for activity in group %>
    <%= render_activity activity %>

附带说明一下,您可以使用我们的 stream_rails library, has some enrichment 工具将活动中的引用转回完整对象。默认情况下,它处理 actor 和对象,这使您可以访问在转储上面的 activity 数据时看到的 activity['actor'].name 之类的东西,但如果您愿意,也可以为元数据项引用其他模型稍后包括那些。