[英]Displaying a list of followers and people following
Hell All, 地狱全部
I am using a ruby gem called socialization ( https://github.com/cmer/socialization ) 我正在使用一个名为socialization的红宝石宝石( https://github.com/cmer/socialization )
I have it working and I am able to follow and unfollow a user and see how many people are following or followed by any user. 我可以使用它,并且可以关注和取消关注某个用户,并查看有多少人关注或关注了某个用户。 the issue is when I want to output the usernames of the followers and following, it only works for user1, and for user 2-4 it will show the usernames in order based on how many followers eg if the table is
问题是,当我想输出关注者及其以下用户的用户名时,它仅适用于user1,而对于用户2-4,它将根据有多少关注者按顺序显示用户名,例如,如果表是
and I ask for the followers for user 3 (knowing user3 as 1 follower) it will print out User1 regardless (i hope this makes sense) 我要求用户3的关注者(知道用户3为1个关注者),无论如何都将打印出用户1(我希望这是有道理的)
Here is my code: 这是我的代码:
Followers method in UsersController UsersController中的Followers方法
def followers
@user = User.find(params[:user])
@followers = @user.followers(User)
@users = User.all
response = {:user => @user, :followers => @followers, :users => @users}
respond_to do |format|
format.html #followers.html.erb
format.xml {render :xml => response}
end
end
Following method in UsersController UsersController中的以下方法
def following
@user = User.find(params[:user])
@following = @user.followers(User)
@users = User.all
response = {:user => @user, :following => @following, :users => @users}
respond_to do |format|
format.html #following.html.erb
format.xml {render :xml => response}
end
end
following View: 正在查看:
<p><u>Following</u></p>
<% (1..(@following.count)).each do |i| %>
<% if @users[i].followed_by?(@user) %>
<p> <%= @users[i].username %> </p>
<% end %>
<% end %>
Followers view 关注者视图
<p><u>Followers</u></p>
<% (1..(@followers.count)).each do |i| %>
<% if @users[i].follows?(@user) %>
<p> <%= @users[i].username %> </p>
<% end %>
<% end %>
My route 我的路线
resources :users do
match 'show/:id' => 'user#show'
collection do
get 'follow'
get 'unfollow'
get 'followers'
get 'following'
end
the only one that seems to work correctly is /users/followers?user=1
唯一似乎正常运行的是
/users/followers?user=1
everything else either shows nothing or the wrong names. 其他所有内容都不显示任何内容或名称错误。
Any help would be appreciated! 任何帮助,将不胜感激!
You're getting all the followers in @followers
, so use that in your view: 您将在
@followers
获得所有关注者,因此请在您的视图中使用它:
<p><u>Followers</u></p>
<% @followers.each do |f| %> # f object is of type User.
# You don't need the if statement that you had,
# because you already know that they are followers.
<p> <%= f.username %> </p>
<% end %>
This will print the username of each follower
. 这将打印每个
follower
的用户名。 The other issues you have are that your followers and following methods do the same thing, check the docs for your gem to figure out how to get following
and then structure your view for that similarly. 您遇到的其他问题是您的关注者和跟踪方法执行相同的操作,检查文档以了解您的宝石,从而确定如何进行
following
,然后以类似的方式构建您的视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.