一部のユーザーに電子メールを送信するための rake タスクを作成しようとしています (cron ジョブで使用するため)。
アプリケーションでは、すべての関数が正常に動作しますが、rake タスクでは、view_context を Prawn オブジェクトに送信して PDF をレンダリングすることができません。Prawn で view_context を使用して、ユーザー名とヘルパー クラスを取得します。
コードに従います:
レーキ タスク:
task :view_missing_emails => :environment do
User.all.each do |user|
LetterMailer.attached_pdf(user.email, view_context).deliver
end
レターメーラーのコード:
class LetterMailer < ActionMailer::Base
default from: "test@example.com"
def attached_pdf(email, view)
@email = email
@view = view
attachments["Letter.pdf"] = LetterPdf.new(@email, @view).render
end
end
レターPDFコード:
class LetterPdf < Prawn::Document
def initialize(email, view)
@email = email
@view = view
if @view.current_user.user_permission.admin?
text "You are an admin"
else
text "You are NOT an admin"
end
text "Your email is: #{@email}"
end
(current_user は Devise メソッドです)
前述したように、Web ページでアプリケーションを実行すると、すべてが正常に機能します。しかし、Rake タスクで次のエラーが発生します。
rake aborted!
undefined local variable or method `view_context' for main:Object
view_context を Rake タスクに渡す方法、または view_context を送信せずにビューとコントローラーのヘルパー メソッドを使用して PDF をレンダリングする別のよりスマートな方法がある場合は、どのようなアイデアがありますか?
ありがとう!