0
votes

Hell All,

I am using a ruby gem called 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 e.g. if the table is

  • user1 follows => user 2
  • user2 follows => user 3

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)

Here is my code:

Followers method in UsersController

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

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

everything else either shows nothing or the wrong names.

Any help would be appreciated!

1

1 Answers

1
votes

You're getting all the followers in @followers, so use that in your view:

<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. 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.