Skip to content

Instantly share code, notes, and snippets.

@fnando
Last active January 3, 2016 08:19
Show Gist options
  • Save fnando/8434842 to your computer and use it in GitHub Desktop.
Save fnando/8434842 to your computer and use it in GitHub Desktop.
Render templates outside the controller in a Rails app

You templates must live in app/views, as usual. To render it, just use something like below.

Renderer.render(template: "message", locals: {name: "John Doe"});
<!-- app/views/message.html.erb -->
Hi there, <%= name %>.
module Renderer
def self.render(options = {})
view = View.new(ActionController::Base.view_paths)
view.extend(ApplicationHelper)
view.render(options)
end
class View < ActionView::Base
include Rails.application.routes.url_helpers
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment