我正在使用名为acts_as_follower
的红宝石宝石我的代码:
user.rb
class User < ApplicationRecord
acts_as_followable
acts_as_follower
end
users_controller.rb
def follow
@user = User.find(params[:id])
current_user.follow(@user)
redirect_to root_path
end
def unfollow
@user = User.find(params[:id])
current_user.stop_following(@user)
redirect_to root_path
end
followers.html.erb
<% @user.followers.each do |user| %>
<div class="panel panel-default col-md-offset-3 col-md-6">
<br>
<div class="panel panel-heading">
<%= avatar_for(user, size: 50) %>
<h1> <%=link_to user.name, user %></h1>
</div>
</div>
的routes.rb
#followers
resources :users do
member do
get :follow
get :unfollow
end
end
如何显示我关注的用户?
答案 0 :(得分:0)
如何显示我关注的用户?
我相信您想要的方法是all_following
user.all_following
返回用户的每个跟随对象的数组,这可以是a 不同对象类型的集合,例如:User,Book
所以,下面应该有效
@user.all_following