我在Heroku上部署了一个带有Heroku调度程序附加组件的Rails应用程序,并遵循以下链接:Heroku Scheduler。我试图做的是在每个月的18日设置以下index
。我的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
答案 0 :(得分:2)
简单的事情;
task :overtime_report => :environment do
if Date.today.day == 18
HospitalBooking.index
end
end
然后每天运行您的日程安排程序。
但是你不想从你的rake任务中调用这样的控制器索引方法。 HospitalBooking将是您所期望的模型,而不是控制器。您最好的选择是将您的电子邮件/生成PDF作为模型中的可调用方法,然后从您的任务中调用它。