在Railscasts之后,我尝试设置计划的 rake 任务以向管理员用户发送电子邮件:
desc 'Testing rake task to generate email'
task :overtime_report => :environment do
hospital_bookings = HospitalBooking.scoped
hospital_booking = hospital_bookings
user = User.where(:roles => :administrator)
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
控制器
class HospitalBookingsController < ApplicationController
#load_and_authorize_resource
before_filter :admin_user, :only => [:index]
def index
@hospital_bookings = HospitalBooking.scoped
system "rake overtime_report Mail_ID=#{params[:id]} &"
flash[:notice] = 'Delivering Overtime Report'
end
如果我朝着正确的方向前进,只需要一点指导。