Ruby-on-rails 将 Rails 视图呈现为电子邮件字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2021740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:06:57  来源:igfitidea点击:

Rendering a Rails view to string for email

ruby-on-rails

提问by nixterrimus

I'd like to send emails from a table every night at midnight, so I've created a model to track all communications to be sent. This Communicationmodel stores who the letter is to, from, and the contents of the letter to be sent as html. The user enters a criteria to match for whom the letter is intended: state, country, favorite color, and so which returns a list of acceptable matches.

我想每天晚上午夜从桌子上发送电子邮件,因此我创建了一个模型来跟踪要发送的所有通信。该Communication模型将信件的收件人、发件人以及要发送的信件内容存储为 html。用户输入一个标准来匹配这封信的目标:州、国家、最喜欢的颜色等等,它返回一个可接受的匹配列表。

This list of acceptable matches is iterated across and a new Communication is generated for each. How do I render the contents of a partial view to a string to store in the database for delivery at midnight.

这个可接受的匹配列表被迭代,并为每个生成一个新的通信。如何将局部视图的内容呈现为字符串以存储在数据库中以便在午夜交付。

@communication = Communication.new(params[:communication])
@communication.body = //Render Partial with local variables to string here


January 8, 2010:I've implemented the solution proposed by neutrino, and my code looks like this:

2010 年 1 月 8 日:我已经实现了 neutrino 提出的解决方案,我的代码如下所示:

@communication.message_body = render_to_string(
  :partial => "letters/small_letter", 
  :locals => { :quote => @quote})

The drawback to this approach is that it requires a context so it must be rendered within a controller, this rules out generating the email using a rake file. As others have pointed out the approach of using a Communication model may introduce some unnecessary complexity. At this time since the conditions I use to generate the list of recipients is never stored I've opted to continue to use a communication model.

这种方法的缺点是它需要一个上下文,因此它必须在控制器中呈现,这排除了使用 rake 文件生成电子邮件的可能性。正如其他人指出的那样,使用通信模型的方法可能会引入一些不必要的复杂性。此时,由于我用于生成收件人列表的条件从未存储过,因此我选择继续使用通信模型。

回答by alex.zherdev

As (almost) always in Rails, there's a method for that: ActionController::Base#render_to_string

就像(几乎)总是在 Rails 中一样,有一种方法可以做到: ActionController::Base#render_to_string

In Rails 3, this method was moved to AbstractController::Rendering#render_to_string.

在 Rails 3 中,这个方法被移到了AbstractController::Rendering#render_to_string.

回答by tadman

You may want to look at techniques to render a template outside of a controller, such as:

您可能想了解在控制器外部渲染模板的技术,例如:

http://geek.swombat.com/rails-rendering-templates-outside-of-a-contro

http://geek.swombat.com/rails-rendering-templates-outside-of-a-contro

That being said, it's probably better to make use of the built-in email sending class ActionMailer::Base which can render templates for you. Storing the parameters in your Communication model is probably better than storing your unrolled HTML, as these parameters can be serialized and later pushed back into a "send_X_notification" type call.

话虽如此,使用内置的电子邮件发送类 ActionMailer::Base 可能会更好,它可以为您呈现模板。将参数存储在您的通信模型中可能比存储您展开的 HTML 更好,因为这些参数可以被序列化,然后被推回到“send_X_notification”类型的调用中。

These can be built using the generator:

这些可以使用生成器构建:

script/generate mailer communications_mailer

回答by fluxsaas

you could try this (in the rails console):

你可以试试这个(在 rails 控制台中):

  class StaticRender < ActionController::Base
    def self.render_erb(template_path, params)
      view = ActionView::Base.new(ActionController::Base.view_paths, {})
      class << view
        include ApplicationHelper
      end
      view.render(:file => "#{template_path}.html.haml", :locals => params, :layout => false)
    end
  end


 StaticRender.render_erb("home", {})

you need to have the view "home" for that....

你需要有“家”的看法......

回答by Nakul

Have you tried action mailer? It will get your views rendered and provide active record support also.

你试过动作邮件吗?它将呈现您的视图并提供活动记录支持。