Nsha
Nsha

Reputation: 35

rails email notification- mailer

I'am new to rails. Users need to receive the email notification after few days only if they haven't read the notification yet. I've created the email notification with Action Mailer but it's not working after added the time format. The email still not deliver to the users after 5 days.

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

Upvotes: 0

Views: 563

Answers (1)

chaitanya
chaitanya

Reputation: 1974

i think correct code would be like this: Need to check each notification with current time

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

or can try other way

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

Upvotes: 1

Related Questions