Skip to content

Instantly share code, notes, and snippets.

@calvincorreli
Created October 17, 2012 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calvincorreli/3903202 to your computer and use it in GitHub Desktop.
Save calvincorreli/3903202 to your computer and use it in GitHub Desktop.
Code to render a view from inside a model
def render_to_string
I18n.with_locale(locale) do
Renderer.render(
file: "purchase_mailer/invoice",
layout: nil,
assigns: { charge: self, purchase: purchase, product: product, account: account, payments: payments }
)
end
end
class Renderer
def self.render(options)
(@@renderer ||= self.new).render(options)
end
def view_context_class
Class.new(ActionView::Base) do
include ApplicationHelper
include EmailHelper
include Rails.application.routes.url_helpers
end
end
def _prefixes
[]
end
def render(options)
view_context_class.new(ActionController::Base.view_paths, options.delete(:assigns), self).render(options)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment