如何在Rails 4中使用Acts_as_follower在配置文件上创建关注/关注者列表

时间:2014-01-15 09:57:39

标签: ruby-on-rails ruby devise ruby-on-rails-4

我已正确设置acts_as_follower,用户可以互相关注,效果很好!

我正在使用设计,但我有一个单独的配置文件控制器,这是我有follow / unfollow方法。

我现在正在尝试获取用户在用户个人资料中关注/关注者的所有人的列表。

我只启动了以下部分,我认为我已经正确设置了控制器部件,我只是不确定如何在视图中获取列表。

Profiles_controller.rb

def _following
   @user = User.find_by_username(params[:username])
   @users = User.find_by_username(params[:username]).all_following
end

def _followers
   @user = User.find_by_username(params[:username])
   @users = User.find_by_username(params[:username]).followers
end

def follow
      @user = User.find(params[:id])

      if current_user
        if current_user == @user
          flash[:error] = "You cannot follow yourself."
        else
          current_user.follow(@user)
          RecommenderMailer.new_follower(@user).deliver if @user.notify_new_follower
         format.html {notice:"You are now following #{@user.full_name}."}
         redirect_to profileshow(@user)
        end
      else
        flash[:error] = "You must <a href='/users/sign_in'>login</a> to follow #{@user.monniker}.".html_safe
      end
    end

    def unfollow
      @user = User.find(params[:id])

      if current_user
        current_user.stop_following(@user)
        flash[:notice] = "You are no longer following #{@user.full_name}."
        redirect_to root_path
      else
        flash[:error] = "You must <a href='/users/sign_in'>login</a> to unfollow #{@user.monniker}.".html_safe
      end

_following.html.erb

<br>
<div class="container">
    <div class="page-header">
        <h1><%= @user.full_name %>
        <% if current_user.full_name == @user.full_name %>

        <% else %>
            <% if current_user.following?(@user) %>
                <%= link_to("Unfollow", unfollow_profile_path(@user), :class => 'btn btn-danger' ) %>
            <% else %>
                <%= link_to("Follow" , follow_profile_path(@user.to_param), :remote => true, :class =>' btn btn-success' )%>
            <% end %>
        <%end %></h1>
    </div>

<% if @users.any? %>
    <% @users.each do |user| %>
        <%= @user.first_name %>
        <hr>
    <% end %>
<% end %>

_followers.html.erb

<br>
<div class="container">
    <div class="page-header">
        <h1><%= @user.full_name %>
        <% if current_user.full_name == @user.full_name %>

        <% else %>
            <% if current_user.following?(@user) %>
                <%= link_to("Unfollow", unfollow_profile_path(@user), :class => 'btn btn-danger' ) %>
            <% else %>
                <%= link_to("Follow" , follow_profile_path(@user.to_param), :remote => true, :class =>' btn btn-success' )%>
            <% end %>
        <%end %></h1>
    </div>

如何在视图中显示关注者/关注者?任何帮助都会很棒!

1 个答案:

答案 0 :(得分:1)

使用此方法收集关注者和关注者的所有记录。而不是使用此对象来显示信息。

@user.all_follows
@user.all_following