2016-10-17 12 views
0

フレンドシップリクエストのために私のレールアプリにhas_friendship宝石を実装しました。私は友人のリクエストが送信され、それが受け入れられたときに電子メール通知が欲しいです(例えば、ユーザーAがBにリクエストを送信し、Bが通知を受け取ります。友達リクエスト:。フレンドリクエストが送信され、レールで受け入れられたときの電子メール通知

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 => '[email protected]' 

    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を使用しています。どんな助けでも大歓迎です。 ありがとう

+3

は、あなたがしようとしactionmailerのコードを表示してください動作するはずです。 – SteveTurczyn

+2

'それは働くことができませんでした.'は非常に曖昧です。あなたはあなたが何を観察し、あなたが観察することを期待しているか教えてください。あなたの設定/環境ファイルにメール設定があることを確認しましたか?あなたはアクションメーラーの新しいですか?もしそうなら、その件に関するRailsのガイドを読んでください:http://guides.rubyonrails.org/action_mailer_basics.html –

+0

申し訳ありませんが、私は実際に端末に何の注意も見ませんでした。私は電子メールの結果を見ることを期待していました。私はユーザーがサインアップするときに電子メールの確認を送信するために使用しているので、設定/環境でsendgridを設定しています。私はこれについてのレールガイドを見ましたが、私は達成しようとしているものに自分のコードを適応させるのにまだ苦労しています – Maurice

答えて

0

あなたの方法にメーラメソッドを追加すると、

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 
+0

私はそれをして、それは私の端末で動作します。私は私のログの電子メールを見ることができますが、何らかの理由で電子メールが生産に送られない(私はHerokuを使用しています)。なぜどんなアイデア? @ user3693398 – Maurice

+0

Nevermind、うまくいきます。メールが来るまでにはちょっと時間がかかります。それを見る時間をとった皆さんに大きな感謝をしました:-) – Maurice

関連する問題