superbot
superbot

Reputation: 461

Sending request/accept notification for friendship

I have a mutual friendship model, where one user requests a friendship and creates a model with user_id of current_user and friend_id of the friend.

Then the friend accepts it and creates another model of the inverse.

Now I am trying to send notification upon both cases. The problem is that @friend (current_user as well) in my code seems to be nil or otherwise just not working.

  def notify_friend_request
    @friend = params[:friend]
    @url  = 'http://localhost:3000'
    @first_name = @friend.first_name
    @last_name = @friend.last_name
    @email = @friend.email
    @sent_user = current_user
    @sent_user_first_name = @sent_user.first_name
    @sent_user_last_name = @sent_user.last_name
    
    mail(to: @email,
         subject: 'You have a new friend request!')

What could be wrong? I'd really appreciate help.

My friendship controller, create method is below. Upon request or acceptance the appropriate mailer method seems to be called (notify_friend_request vs. accept)

  def create
    @inviting_user = User.find(current_user.id)
    @friend = User.find(params[:friend_id])
    @friendship = current_user.friendships.build(:friend_id => params[:friend_id])
    if @friendship.save
      if @friend.friends.include? current_user
        UserMailer.with(friendship: @friendship).notify_friend_accept.deliver_later
      else 
        UserMailer.with(friendship: @friendship).notify_friend_request.deliver_later
      end
      

Upvotes: 0

Views: 188

Answers (1)

superbot
superbot

Reputation: 461

I solved it with this code. Posting a question really cleared up my head:

  def notify_friend_request
    @friendship = params[:friendship]
    @url  = 'http://localhost:3000'
    @first_name = User.find(@friendship.user_id).first_name
    @last_name = User.find(@friendship.user_id).last_name
    @sent_user = User.find(@friendship.friend_id)
    @sent_user_first_name = @sent_user.first_name
    @sent_user_last_name = @sent_user.last_name
    @email = @sent_user.email
    
    mail(to: @email,
         subject: 'You have a new friend request!')
  end

Upvotes: 0

Related Questions