title: Rendering a Rails view to string for email
body: <p>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 <code>Communication</code> model 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.</p>
  
  <p>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.</p>
  
  <pre><code>@communication = Communication.new(params[:communication])
  @communication.body = //Render Partial with local variables to string here
  </code></pre>
  
  <hr>
  
  <p><strong>January 8, 2010:</strong> I've implemented the solution proposed by neutrino, and my code looks like this:</p>
  
  <pre><code>@communication.message_body = render_to_string(
    :partial =&gt; "letters/small_letter", 
    :locals =&gt; { :quote =&gt; @quote})
  </code></pre>
  
  <p>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.</p>