我想在Mail erb模板中用作body.当我在Pony gem上设置它时,它可以工作.
post 'test_mailer' do
Mail.deliver do
to ['test1@me.com', 'test2@me.com']
from 'you@you.com'
subject 'testing'
body erb(:test_mailer) # this isn't working
end
end
private
fields = [1, 2] # some array
Run Code Online (Sandbox Code Playgroud)
ERB文件
<% fields.each do |f| %>
<%= f %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
假设您使用Pony的原始Sinatra路线看起来像这样:
post 'test_mailer' do
Pony.mail :to => ['test1@me.com', 'test2@me.com'],
:from => 'you@you.com',
:subject => 'testing',
:body => erb(:test_mailer)
end
Run Code Online (Sandbox Code Playgroud)
您可以看到此处的电子邮件属性由Hash指定.当切换到使用Mail gem时,它的属性由在特定上下文中调用的块定义,以便这些特殊方法可用.
我认为问题可能与调用erb块内部有关.您可以尝试以下几种方法:
尝试以可以传递到块中的方式生成ERB:
post 'test_mailer' do
email_body = erb :test_mailer, locals: {fields: fields}
Mail.deliver do
to ['test1@me.com', 'test2@me.com']
from 'you@you.com'
subject 'testing'
body email_body
end
end
Run Code Online (Sandbox Code Playgroud)
或者全局调用ERB而不是使用sinatra帮助器:
post 'test_mailer' do
context = binding
Mail.deliver do
to ['test1@me.com', 'test2@me.com']
from 'you@you.com'
subject 'testing'
body ERB.new(File.read('views/test_mailer.erb')).result(context)
end
end
Run Code Online (Sandbox Code Playgroud)