[英]Attaching AXLSX view to mail Rails Mailer
According to the GitHub Page for the axlsx
gem I should use this syntax to render a xlsx view to a file and attach it: 根据axlsx
gem的GitHub页面 ,我应该使用这种语法将xlsx视图呈现给文件并附加它:
xlsx = render_to_string handlers: [:axlsx], formats: [:xlsx], template: "users/export", locals: {users: users}
attachments["Users.xlsx"] = {mime_type: Mime::XLSX, content: xlsx}
Here is my mail method: 这是我的邮件方法:
xlsx = render_to_string(handlers: [:axlsx], formats: [:xlsx], template: 'v1/reports/reportxyz', params: {start_date: '2016-09-12', period: 'weekly'})
attachments["report.xlsx"] = {content: xlsx, mime_type: Mime::XLSX}
mail(to: "my@email.address", subject: "Report", format: "text")
However I get this error when I try and call the mailer method: 但是,当我尝试调用邮件程序方法时,我收到此错误:
ActionView::MissingTemplate: Missing template layouts/mailer with {:locale=>[:en], :formats=>[:xlsx], :variants=>[], :handlers=>[:axlsx]}. Searched in:
* "path/to/project/app/views"
Why is the render_to_string
method affecting what the mailer view the mailer is trying to render? 为什么render_to_string
方法会影响邮件程序正在尝试呈现的邮件程序? locgially I don't have a mailer.xlsx.axlsx
file in my app/views/layouts
folder but rather the mailer.text.erb
I am trying to use as with other emails. locgially我的app/views/layouts
文件夹中没有mailer.xlsx.axlsx
文件,而是mailer.text.erb
我试图像其他电子邮件一样使用。
EDIT 编辑
I changed the render line to xlsx = render_to_string(template: 'v1/reports/azamara_social', params: {start_date: '2016-09-12', period: 'weekly'})
我将渲染线更改为xlsx = render_to_string(template: 'v1/reports/azamara_social', params: {start_date: '2016-09-12', period: 'weekly'})
And now it seems to try and render the xlsx
view but of course gets nil:NilClass
errors when the xlsx
view tries to reference instance variables defined in the reports controller. 现在它似乎尝试渲染xlsx
视图但当然得到nil:NilClass
当xlsx
视图尝试引用报表控制器中定义的实例变量时出现nil:NilClass
错误。
Have you tried passing layout: false? 你试过传递布局:假吗? What versions of axlsx, axlsx_rails, rails, and rubyzip are you using? 您使用的是axlsx,axlsx_rails,rails和rubyzip的哪些版本?
In the end it all came down to moving the controller code into a lib file. 最后,所有这些都归结为将控制器代码移动到lib文件中。 This way I call it in the controller to get the data if it needs to be rendered via web-requests as well as via the Mailer method where I recreate the @variables
the view template is looking for. 这样我就可以在控制器中调用它来获取数据,如果它需要通过Web请求以及通过Mailer方法呈现,我将重新创建视图模板正在寻找的@variables
。
Here is the finished salient parts of the report mailer method: 以下是报告邮件方法的完成部分:
data = ReportUtils.get_data(args)
xlsx = render_to_string(template: 'path/to/report.xlsx', locals: {:@period => period, :@date_ranges => data[:date_ranges], :@data => data[:data]})
attachments["report.xlsx"] = {content: xlsx, mime_type: Mime::XLSX}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.