Lewis Frost
Lewis Frost

Reputation: 557

How do I create a list of following/followers on profile with Acts_as_follower in Rails 4

I have set up acts_as_follower correctly and users can follow each other, this works great!

I am using devise but I have a separate profiles controller which is where I have the follow/unfollow methods.

I am now trying to get a list of all the people a user is following/followers on a users' profile.

I have only started only the Following part and I think I have set up the controller part properly, i'm just not sure how to get the list in the view.

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>

How Do I show the followers/following in the views? Any help will be great!

Upvotes: 1

Views: 1144

Answers (1)

Nitin Jain
Nitin Jain

Reputation: 3083

use this to collect all record for follower and following . than use this object to show the information.

@user.all_follows
@user.all_following

Upvotes: 1

Related Questions