Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rendering a Rails view to string for email

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 Communication 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.

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:

@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.

like image 821
nixterrimus Avatar asked Jan 07 '10 16:01

nixterrimus


3 Answers

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

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

like image 128
alex.zherdev Avatar answered Nov 01 '22 03:11

alex.zherdev


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

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.

These can be built using the generator:

script/generate mailer communications_mailer
like image 12
tadman Avatar answered Nov 01 '22 03:11

tadman


you could try this (in the rails console):

  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....

like image 5
fluxsaas Avatar answered Nov 01 '22 03:11

fluxsaas