在 Rails 中渲染 HTML 而不实际显示它
我当前的项目要求我组装一个包含 HTML 和纯文本模板的 .zip 文件,供用户下载,以便导入到电子邮件营销程序中。
我继承了这个项目,目前代码使用“假”模型(即不直接与数据库表相关的模型),其中它将整个模板存储在字符串中,使用动态变量来填充某些地区。然后,“假”模型有一个创建 zip 文件的方法。
在我看来,必须有更好的方法来做到这一点。我想知道是否有办法将模板移动到 .erb/haml 文件中,然后编写一个方法来填充该文件以准备压缩?基本上,有没有一种方法可以渲染 HTML 和文本文件,而无需实际显示它们?
感谢您的任何帮助。
My current project requires me to assemble a .zip file containing HTML and text-only templates for a user to download, for importing into an email marketing program.
I've inherited this project, and currently the code uses a "fake" model (that is a model that does not directly correlate to a database table), in which it stores the entire template in a string, using dynamic variables to populate certain areas. The "fake" model then has a method for creating a zip file.
It seems to me that there has to be a better way to do this. I was wondering if there was a way to move the template into a .erb/haml file, and then write a method that would populate the file in preparation for being zipped up? Basically, is there a way to render an HTML and text file, without actually having to display them?
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需编写操作和视图来正常渲染 html 和文本,然后使用 render_to_string 方法将内容分配给字符串
http://apidock.com/rails/ActionController/Base/render_to_string
然后您可以运行 Model 方法(希望是类方法)以使用实例变量中现在的内容创建 zip 文件
Just write the action and view to render the html and text as normal then use the render_to_string method to assign the content to strings
http://apidock.com/rails/ActionController/Base/render_to_string
Then you can run the Model method (hopefully a Class method) to create the zip file using the content you now have in instance variables
请查看 cells 插件。
Please have a look at the cells plugin.