0

我正在尝试按高中分组用户朋友,用户使用他的 Facebook 帐户登录,我使用 Facebook 图表来获取他所有的朋友。

此处的代码首先输出/打印所有用户朋友,并按高中打印每个朋友的组。我不想要那个,我只希望它输出/打印组。

<ul class="friends-list">
<% current_user.friends.group_by(&:highschool_name).map do |hsname, friends| %>
   <li><%= hsname %></li> 
   <% friends.map do |friend| %>
   <%= image_tag(friend.image) %> 
 <% end %>
 <% end %>
</ul>

用户表:

[id, name, image, location, highschool_name, highschool_year, college_name, college_year, graduteschool_name, graduate_year ]

朋友表:

[id, uid, name, image, higschool_name, college_name, graduateschool_name, user_id]

当前输出的详细信息:

Started GET "/users/1" for 127.0.0.1 at 2012-07-16 01:10:17 +0200
Processing by UsersController#show as HTML
  Parameters: {"id"=>"1"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", "1"]]
  Rendered users/_profile_content.html.erb (0.6ms)
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Friend Load (9.2ms)  SELECT "friends".* FROM "friends" WHERE "friends"."user_id" = 1
  Rendered users/show.html.erb within layouts/application (287.4ms)
  Rendered layouts/_shim.html.erb (0.1ms)
  Rendered layouts/_header.html.erb (2.3ms)
  Rendered layouts/_footer.html.erb (0.6ms)
Completed 200 OK in 321ms (Views: 309.3ms | ActiveRecord: 9.8ms)

输出图像:http: //postimage.org/image/tgyl9f27n/

4

2 回答 2

0

我不知道我是否完全理解你的问题,但听起来你想列出每个组的朋友。如果这是正确的,我认为您不需要第二个地图功能。

我认为它可能应该是一个 for 循环。

  <% current_user.friends.group_by(&:highschool_name).map do |hsname, friends| %>

       <li><%= hsname %></li> 
        <%for friend in friends%>
           <%= image_tag(friend.image) %> 
      <% end %>
   <% end %>

希望有帮助

于 2012-07-16T18:57:20.827 回答
0

问题: 数据库中的某些值是空的。给出错误

解决方案:

<% next if hsname.blank? %>
于 2012-07-19T23:49:56.397 回答