2017-03-23 7 views
0

初めてのレールです。ユーザーは通知をまだ読まなかった場合にのみ数日後に電子メール通知を受け取る必要があります。私はAction Mailerで電子メール通知を作成しましたが、時刻形式を追加しても機能しません。電子メールはまだ5日後にユーザーに配信されません。レールのメール通知 - メーラー

class NotificationsController < ApplicationController 

def show 
    @notification = current_user.notifications.find_by_id(params[:id]) 

    if current_user.notifications.unread > 5.days.ago 
    UserMailer.try_notif(@notification, @post, @user).deliver 
    end 
end 

def update 
    @notification = current_user.notifications.find_by_id(params[:id]) 
    @notification.update(read: true) 
    redirect_to post_path(@notification.post) 
end 

def read_all 
    current_user.notifications.unread.update_all(read: true) 
end 
end 

user_mailer.rb

class UserMailer < ApplicationMailer 

def try_notif(notification, post, user) 
    @notification = notification 
    @post = post 
    @user = user 
    mail(to: @post.user.email, subject: 'New Notification') 
end 
end 
+1

あなたはどんなエラーを受けていますか? 'current_user.notifications.unread'が日付を返すかどうかチェックします。そうでなければ比較は失敗します。 – Sravan

+1

期待されるワークフローは何ですか?現時点では、ユーザーが1つの特定の通知を見た瞬間にメールが送信されます。彼が通知を見ないなら、彼はその電子メールをまったく受け取らないでしょう。さらに、すでに通知を見ている場合は、通知に関する電子メールを送信するのは何ですか? – spickermann

+0

5.days.agoを5.minutes.agoに変更しようとしたときに電子メールを受信しませんでした。 – Nsha

答えて

1

私は、正しいコードは次のようになると思います。

if current_user.notifications.unread 
    current_user.notifications.each do |notification| 
    if Time.current - notification.created_at > 120 #just checking in hours 
    #send reminder 
    end 
    end 
end 

かは、他の方法を試すことができ、現在の時刻と各通知をチェックする必要があり

@notifications = Notification.where(created_at: 5.days.ago..Time.current, unread: true) 
if @notifications 
    @notifications.each do |notification| 
    #send notification 
    end 
end 
+0

2番目の例の変更を.. current_user.notifications.where(..) – gates

+0

ありがとうあなたの提案。 Btwはまだメールを受け取っていませんでした。 – Nsha

関連する問題