在 rails 内发送并接受好友请求时的电子邮件通知

Email notification when friend request is sent and accepted in rails

我在我的 rails 应用程序中实现了 has_friendship gem 用于交友请求。我现在想要在发送好友请求和接受好友请求时收到电子邮件通知(例如,用户 A 向 B 发送请求,B 收到通知。用户 B 接受请求,A 收到通知。这些是我发送和接受的控制器操作好友请求:

  def add_friend
    if current_user.friend_request(@friend)
      redirect_to my_friends_path, notice: "Friend request successfully sent."
    else
      redirect_to my_friends_path, flash[:error] = "There was an error sending the friend request"
    end
  end

  def accept_friend
    if current_user.accept_request(@friend)
      redirect_to my_friends_path, notice: "Friend request successfully accepted."
    else
      redirect_to my_friends_path, flash[:error] = "There was an error accepting the friend request."
    end
  end

我试用了 actionmailer,但无法正常工作。 到目前为止,这是我对邮件程序方法的了解:

class FriendshipNotifier < ApplicationMailer
  default :from => 'do-not-reply@example.com'

  def sent_friend_requests(@friend)
    @friend = friend
    mail( :to => @friend.email,
          :subject => 'You have received a friend request.' )
     end
  end

  def accepted_friend_requests(@friend)
    @friend = friend
    mail( :to => @friend.email,
          :subject => 'Your friend request has been accepted.' )
    end
  end
end

我正在使用 Sendgrid。任何帮助将非常感激。 谢谢

将邮件程序方法添加到您的方法中应该可行

def add_friend
  if current_user.friend_request(@friend)
    FriendshipNotifier.send_friend_requests(@friend)
    redirect_to my_friends_path, notice: "Friend request successfully sent."
  else
    redirect_to my_friends_path, flash[:error] = "There was an error sending the friend request"
  end
end

def accept_friend
  if current_user.accept_request(@friend)
    FriendshipNotifier.accepted_friend_requests(@friend)
    redirect_to my_friends_path, notice: "Friend request successfully accepted."
  else
    redirect_to my_friends_path, flash[:error] = "There was an error accepting the friend request."
  end
end