gpt4 book ai didi

sinatra - 未定义的方法 `haml' 在来自计划作业的 Pony 邮件中渲染 haml 时

转载 作者:行者123 更新时间:2023-12-04 23:26:57 24 4
gpt4 key购买 nike

我有一个 sinatra 应用程序,它执行 cucumber 测试并发送带有结果的电子邮件通知。电子邮件 gem 是 Pony,此通知有一个 haml 模板。

此逻辑适用于路线:

require 'sinatra'
require 'haml'
require 'pony'

get "/execute_all/?" do
execute_all_tests()
Pony.mail :to => "recipients@email.com",
:from => "do-not-reply@email.com",
:subject => "Test results,
:html_body => haml(:email_layout)

redirect "/"

end

但是,当我将预定作业与 rufus 调度程序一起用于这些操作时,出现以下异常:
scheduler caught exception:
undefined method `haml' for main:Object

代码是来自路线的 copypasta:
   scheduler = Rufus::Scheduler.start_new
scheduler.every '2h' do
execute_all_tests()
Pony.mail :to => "recipients@email.com",
:from => "do-not-reply@email.com",
:subject => "Test results,
:html_body => haml(:email_layout)
end

所有两个方法都在同一个文件中,执行该文件以运行 Sinatra 应用程序。

如何摆脱此异常,并按计划作业发送带有haml模板的电子邮件?

最佳答案

haml方法仅在对 Sinatra 的请求的上下文中可用(它是 Sinatra::Base 上的实例方法),并且不能在 Sinatra 请求之外使用。

为了渲染一个 Haml 模板,你可以直接使用 Haml API:

haml_template = File.read(File.join(settings.views, 'email_layout.haml'))

scheduler = Rufus::Scheduler.start_new
scheduler.every '2h' do
execute_all_tests()
Pony.mail :to => "recipients@email.com",
:from => "do-not-reply@email.com",
:subject => "Test results",
:html_body => Haml::Engine.new(haml_template).render(binding)
end
end

这里我假设 execute_all_tests正在使用 Haml 模板中引用的实例变量,因此我们传递了 binding到哈姆尔 render方法来访问这些变量。您可能还需要将您需要的任何 Haml 选项作为第二个参数传递给 new .

关于sinatra - 未定义的方法 `haml' 在来自计划作业的 Pony 邮件中渲染 haml 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11387479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com