发送请求/接受通知以建立友谊

时间:2020-10-23 14:03:58

标签: ruby-on-rails ruby devise

我有一个共同的友谊模型,其中一个用户请求一个友谊,并使用current_user的user_id和朋友的friend_id创建一个模型。

然后朋友接受它并创建另一个逆模型。

现在,我正在尝试针对这两种情况发送通知。问题是我的代码中的@friend(也包括current_user)似乎为零,否则将无法正常工作。

  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!')

可能是什么问题?非常感谢您的帮助。

我的友情控制器的创建方法如下。根据请求或接受,似乎调用了适当的邮件方法(notify_friend_request与接受)

  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
      

1 个答案:

答案 0 :(得分:0)

我用这段代码解决了它。发表问题确实让我头脑清醒:

  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