2017-03-23 1 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

예상되는 워크 플로우는 무엇입니까? 사용자가 하나의 특정 알림을 보는 순간 메일이 전송됩니다. 알림을 보지 않으면 이메일을 전혀받지 못합니다. 또한 알림을 이미보고있는 경우 알림에 대한 전자 메일을 보내는 요령은 무엇입니까? – 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

두 번째 예에서 수정할 수 있습니다. current_user.notifications.where (..) – gates

+0

고마워요. 당신의 제안. Btw는 여전히 이메일을받지 못했습니다. – Nsha

관련 문제