Reputation: 91
I'm new into rails and I want my users to be able to befriend each other so i found the gem has_friendship github: has_friendship. I did the instructions and kinda tested everything in the rails c console. And everything worked fine. But now im kinda confused how to handle it in my application, and how to integrate it to my application. Do i need an extra friendship controller/model/view or where can i define the methods like @user1.friend_request(@user2)
and how do i get the id of user2?
Thanks alot!
Update
for example: i thought about displaying every user in my user index.html.erb with a link. But how am i able to trigger the friend_request
method by pressing this link?
<% @users.each do |user| %>
<h1><%= user.username %> <%= link_to "Add Friend", user_add_friend_path(:user_id) %></h1>
<%end%>
i also edited in my routes.rb
resources :users do
get 'add_friend'
end
and made a method in my users_controller.rb
def add_friend
if current_user.friend_request(@friend)
redirect_to users_path, notice: "Friend request successfully sent."
else
redirect_to users_path, notice: "There was an error sending the friend request"
end
end
now i am getting a noMethod error because i did not define @friend yet. But how do i get the User i want to be befriended with?
Upvotes: 0
Views: 581
Reputation: 277
Cool - sounds like a fun gem! It looks like after you run the has_friendship
generator, you need to run a migration, which will alter your schema tables and you'll need to add has_friendship
to your User
model. If you can share some more code, then it will be easier for people to offer assistance.
Upvotes: 0
Reputation: 657
You do not need any other model, only the user's, the gem generates a model of friendship
Simply drop in has_friendship to a model user:
class User <ActiveRecord :: Base
has_friendship
end
Methods:
# @mac sends a friend request to @dee
@ mac.friend_request (@dee)
# @dee can accept the friend request
@ dee.accept_request (@mac)
# @dee can also decline the friend request
@ dee.decline_request (@mac)
# @dee removes @mac from its friends
@ dee.remove_friend (@mac)
They are generated by the gem
Upvotes: 1