我在 Heroku 上部署了一个带有 Heroku 调度程序插件的 Rails 应用程序,并点击以下链接:Heroku Scheduler。我正在尝试做的是将以下内容设置为index
在每个月的 18 日运行。我的index
方法如下所示:
def index
@hospital_bookings = HospitalBooking.scoped
hospital_booking = @hospital_bookings
@user = current_user
if params[:format] == "pdf"
@hospital_bookings = @hospital_bookings.where(:day => Date.today.beginning_of_month..Date.today.end_of_month)
end
respond_to do |format|
format.html
format.pdf do
render :pdf => "#{Date.today.strftime("%B")} Overtime Report",
:header => {:html => {:template => 'layouts/pdf.html.erb'}}
OvertimeMailer.overtime_pdf(@user, hospital_booking).deliver
end
end
end
因此,当 rake 任务在每个月的 18 日运行时,这将触发我的 OvertimeMailer 并向用户发送电子邮件。我目前在我的scheduler.rake
task :overtime_report => :environment do
if Date.today.??? # Date.today.wday == 5
HospitalBooking.index
end
end
我知道上面的 rake 任务是错误的。但我试图在这些方面取得一些成就
更新
class OvertimeMailer < ActionMailer::Base
default :from => DEFAULT_FROM
def overtime_pdf(user, hospital_booking)
@hospital_bookings = hospital_booking
@user = user
mail(:subject => "Overtime", :to => user.email) do |format|
format.text # renders overtime_pdf.text.erb for body of email
format.pdf do
attachments["hospital_bookings.pdf"] = WickedPdf.new.pdf_from_string(
render_to_string(:pdf => "overtime",:template => 'hospital_bookings/index.pdf.erb', :layouts => "pdf.html")
)
end
end
end
end