Harry B.
Harry B.

Reputation: 421

Rails: Display pending friend requests in view

I am trying to to have the words Pedning Request show up next to a user in the user index if the current user has requested them or if the current user has been requested by them. I have a feeling something is wring with my erb, or I am just taking the wrong approach completely. When I click the add friend link, I want it to redirect back to the index page with the updated status of the request but it still displays the add friend link.

index.html.erb

Facebook Users:
<ul><% @users.each do |user| %>

    <% if current_user == user || current_user.friends.include?(user) %>
        <li><%= link_to user.email, user_path(user) %></li>
<% current_user.friend_requests.each do |request| %>
    <% if request.friend_id == user.id  %>
    <li><%= link_to user.email, user_path(user) %> 
        | Pending Request</li> 
        <% end %>
        <% end %>
    <% else %>
        <li><%= link_to user.email, user_path(user) %> |
        <%= link_to "Add Friend", friend_requests_path(friend_id: user.id), method: :post %>
        </li>   
    <% end %>
 <% end %>
 </ul>

 <%= link_to 'Back to your profile', user_path(current_user) %>


<%= render 'shared/error_messages', object: current_user %>

<%= params.inspect %>

   class FriendRequestsController < ApplicationController
before_action :set_friend_request, except: [:index, :create]

def index
    @incoming = FriendRequest.where(friend: current_user)
    @outgoing = current_user.friend_requests
end

def create
    @user = User.find(current_user)
    @friend = User.find(params[:friend_id])
    @friend_request = current_user.friend_requests.new(friend_id: @friend.id)

    if @friend_request.save
        flash[:notice] = "Friend request sent!"
        redirect_to users_path
    else
        flash[:notice] = "Unable to request friend"
        redirect_to users_path
    end
end

def update
    friend_email = User.find(@friend_request.friend_id).email

    @friend_request = FriendRequest.find_by_user_id_and_friend_id(params[:friend_id], params[:id])
    if @friend_request.accept
        flash[:notice] = "You and #{friend_email} are now friends!"
        redirect_to user_path(current_user)
    end

end

def destroy
    if @friend_request.destroy
        flash[:notice] = "Request Declined"
        redirect_to user_path(current_user)
    end
end


private

def set_friend_request
    @friend_request = FriendRequest.find_by_user_id_and_friend_id(params[:friend_id], params[:id])
end
end

Upvotes: 0

Views: 446

Answers (1)

SteveTurczyn
SteveTurczyn

Reputation: 36860

The if block starting with...

<% if request.friend_id == user.id  %>

... is INSIDE the 'if' block starting with...

<% if current_user == user || current_user.friends.include?(user) %>

So basically the "Pending Request" can only be shown if the user is yourself (the current user) or if the user is already your friend. That doesn't make sense, of course.

Better to move the code after the first if's elseand you don't really need to loop through all the requests with each, just map the friend_id's

<ul>
<% @users.each do |user| %>
  <% if current_user == user || current_user.friends.include?(user) %>
    <li><%= link_to user.email, user_path(user) %></li>
  <% else %>
    <% if current_user.friend_requests.pluck(:friend_id).include? user.id %>
      <li><%= link_to user.email, user_path(user) %> 
          | Pending Request</li> 
    <% else %>
      <li><%= link_to user.email, user_path(user) %> |
      <%= link_to "Add Friend", friend_requests_path(friend_id: user.id), method: :post %>
      </li>   
    <% end %>
  <% end %>
<% end %>
</ul>

Upvotes: 1

Related Questions