Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Setup of User Index Page that Lists Only Users who are not currently Friends with the Current_User

Hi,

I am trying to set up a user index page which will include a table that lists all the site users alphabetically by profile name (excluding the currently logged in user) and then in the next column will list either the friendship state of that user (i.e. requested, assigned, pending or blocked as it relates to the current_user) or, for users who do not yet have a friendship state with the current_user, a button that will allow them to request the friendship. I have filtered out the current_user from all users via the user controller:

def index
@users = (current_user.blank? ? User.all : User.find(:all, :conditions => ["id != ?", current_user.id]))
end

In the user/index.html.erb file I currently have the text below. This somewhat works in the browser because it produces a table with a list of all users (except the current_user) and a button next to each user profile name that says "Add Connection". The button does work to connect with the user listed next to it, so that's positive. But clearly the "unless : else" qualifications are not working nor is the listing of the user_friendship state. Furthermore, I have not yet been able to figure out how to sort this alphabetically nor have my efforts to paginate this page using "will-paginate" worked either.

Any input or suggestions for how to adjust my code would be much appreciated!

Thanks, L

views/users/index.html.erb file:

<h1>Add Connections</h1>

<table class="table table-hover">
  <thead>
    <tr>
      <th><strong>Profile Name</strong></th>
    </tr>
  </thead>

  <tbody>
    <% @users.each do |user| %>
        <tr>
          <td><%= user.profile_name %></td>
      <td>
        <% unless @user == current_user || current_user.requested_friends.include?(@user) || current_user.accepted_friends.include?(@user) || current_user.pending_friends.include?(@user) || current_user.blocked_friends.include?(@user) %>
          <%= link_to "Add Connection", new_user_friendship_path(:friend_id => user), :method => :post %>
        <% else %>
          <%= user.user_friendship.state %>
        <% end %>
       </td>
        </tr>
     <% end %>
   </tbody>
 </table>

 <br />

1 Answer

try something like this:

 def index
  @users = User.where(['id <> ?', current_user.id]).order("profile_name ASC")
 end

this will return a list of users based on profile name sorted and it will exclude the current user