簡體   English   中英

如何在耙任務中使用設計助手?

[英]How to use devise helpers in a rake task?

我的任務將執行以下操作:

  • 檢查用戶是否登錄,並將其通知管理員

    • 如果為true =>不執行任何操作

    • 如果為false =>向管理員發送電子郵件

我希望此任務每小時執行一次(它將與Heroku Scheduler附加組件一起安排)

這里的問題是您不能使用user_signed_in? current_user在您編寫的任何rake文件中。 此外,我無法將這些值中的任何一個作為參數傳遞,因為它將從Heroku Scheduler的儀表板中調用。

我的一些代碼在這里:

lib /任務/scheduler.rake

task :send_notification => :environment do
  if user_signed_in?
    puts "No need to email"
  else
    puts "Sending email"
    u=User.find(132)   #get an admin user
    ModelMailer.time_notification(u).deliver
  end
end

錯誤:

rake aborted!
NoMethodError: undefined method `user_signed_in?' for main:Object

提前致謝!

Devise的默認配置不允許您跟蹤哪些用戶在線和離線。 幫助程序current_useruser_signed_in? 與當前會話有關,並且僅在用戶請求期間有效。 由於在rake任務中沒有請求,因此它們沒有用。

您可能會在數據庫中添加一列,以跟蹤用戶是否已登錄或注銷,但是devise中沒有此功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM