I am using Amistad to implement the friendship model. I have five actions in the friendships_controller
: index
, request_friend
, approve_friend
, remove_friend
, block_friend
.
I am not sure on how to define the routes in the routes file, I tried using resources :friendships
but am unable to make a friend request from the user profile.
Here are the files:
routes.rb:
resources :friendships
friendships_controller.rb:
class FriendshipsController < ApplicationController
before_filter :authenticate_user!
def index
@friends = current_user.friends
@pending_invited_by = current_user.pending_invited_by
@pending_invited = current_user.pending_invited
end
def request_friend
@friend = User.find(params[:id])
#if !@friend
# redirect_to :back
#end
current_user.invite @friend
redirect_to :back
end
def approve_friend
@friend = User.find(params[:id])
current_user.approve @friend
redirect_to :back
end
def remove_friend
@friend = User.find(params[:id])
current_user.remove_friendship @friend
redirect_to :back
end
def block_friend
@blocking = User.find(params[:id])
current_user.block @blocking
redirect_to :back
end
end
I want User A to be able to send User B a friend request and User B to either accept it, delete it or ignore it.
Here is my show.html.erb
<span>
<% if @user == current_user %>
<%= link_to 'Edit Profile', edit_profile_path(@user.user_name),class: 'btn edit-button' %>
<% elsif current_user.friend_with? @user %>Already Friends!
<%= button_to "Delete!",friendship_path(id: @user.id, friendship_action: 'remove_friendship'), method: :delete, class: "btn btn-primary" %>
<% elsif current_user.invited? @user %>Friend request sent!
<%= button_to "Unsend!", friendship_path(id: @user.id), :method => :delete, class: "btn btn-small btn-primary" %>
<% elsif current_user.invited_by? @user %>
<%= button_to "Accept!", friendship_path(@user.id, friendship_action: 'approve'), :method => :patch, class: "btn btn-small btn-primary" %>
<%= button_to "Reject!", friendship_path(@user.id, friendship_action: 'remove_friendship'), :method => :delete, class: "btn btn-small btn-primary" %>
<% else %>
<%= form_tag friendship_path(@user.id,friendship_action: 'invite') do %>
<%= submit_tag "Request Friend", class: "btn btn-primary" %>
<% end %>
<% end %>
</span>
I dont know why the values are not being inserted when i press the button.But when i do it manually via the console they are being inserted.