Reputation: 117
I am attempting to enable a user to change the status of incoming friend requests from "unaccepted" to "accepted"
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
has_many :places
has_many :sent_friendships, class_name: "Friendship" , foreign_key: :sender_id
has_many :received_friendships, class_name: "Friendship", foreign_key: :receiver_id
class Friendship < ActiveRecord::Base
belongs_to :sender, class_name: "User"
belongs_to :receiver, class_name: "User"
end
<h1>Incoming Friend Requests</h1>
<% current_user.received_friendships.each do |friendship| %>
<%= friendship.sender.name %>
<%= link_to "Accept", user_accept_path(current_user), class: 'btn btn-success', method: :post %>
<% end %>
class FriendshipsController < ApplicationController
def accept
@user = User.find(params[:user_id])
end
end
Where is where I am stumbling because I am unsure as to how best to plus in the sender id and receiver id to update the proper line on the Friendship model table.
Any help is appreciated.
Upvotes: 0
Views: 39
Reputation: 4307
As every your controller has access to current_user
variable, you may not pass it as parameter, but you should pass a friendship.sender
to it.
Then depending on you routes and architecture you may do one of the following:
Create a new Frienship
class FriendshipsController < ApplicationController
def accept
@user = User.find(params[:id])
friendship.create sender_id: current_user.id, reciever_id: @user.id
# or
# friendship.create sender_id: current_user.id, reciever_id: params[:id]
# then rdictect to somewhere
redirect ...
end
end
If you have additional fields in the Frienship model like - accepted, you may update. Then you find a created Frienship model during send of friendship invitation and update the field.
class FriendshipsController < ApplicationController
def accept
@user = User.find(params[:id])
friendship = Frienship.where("sender_id = ? and reciever_id = ?",
# sender was passed as an argument to link_to in the view
@user.id, current_user.id)
friendship.accept = true
friendship.save!
# then rdictect to somewhere
redirect ...
end
end
Upvotes: 1
Reputation: 3458
Your accept
method should basically do a set of things:
devise
has current_user
helper method accessible in controller)received_friendship
to know which particular user is going to be a new friend for current_user
friendship
object (from step #2) to have acceptance
(or whatever you named it) attribute set to accepted
.As you already know, first step does not require any coding from you. The second one should be as simple as setting a local (or @instance) variable based on sender_id
of friendship request (or id
of particular friendship
, which would be even better). And the last one would look something like
friendship.acceptance = "accepted"
if friendship.save
# TODO: redirect to somewhere with a successful notice
else
# TODO: render users/show template once again with an error
end
One more hint: note that your acceptance link does not pass data required at step #2:
<%= link_to "Accept", user_accept_path(current_user) %>
Upvotes: 1