2

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

そのため、レーキ タスクが毎月 18 日に実行されると、これにより OvertimeMailer が起動され、ユーザーにメールが送信されます。私は現在、私の中にいますscheduler.rake

task :overtime_report => :environment do
  if Date.today.??? # Date.today.wday == 5
  HospitalBooking.index
  end
end

上記のレーキタスクが間違っていることは知っています。しかし、これらの線に沿って何かを達成しようとしています

アップデート

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 
4

1 に答える 1

2

のような単純なもの;

task :overtime_report => :environment do
  if Date.today.day == 18
    HospitalBooking.index
  end
end

そして、スケジューラを毎日実行します。

しかし、rake タスクからこのようにコントローラーの index メソッドを呼び出したくはありません。HospitalBooking はモデルであり、予想どおりコントローラーではありません。最善の選択肢は、メール/生成 PDF を呼び出し可能なメソッドとしてモデルに配置し、それをタスクから呼び出すことです。

于 2013-03-25T20:14:37.667 に答える