Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Render a template outside of Rails
require 'abstract_controller'
require 'action_controller'
require 'action_view'
require 'active_record'
# require any helpers
require './app/helpers/application_helper'
# active record only if data is here
require './app/models/widget'
adapter: 'sqlite3',
database: 'db/development.sqlite3'
ActionController::Base.prepend_view_path "./app/views/"
# place data in view_assigns
view_assigns = {widgets: Widget.all}
av =, view_assigns)
av.class_eval do
# include any needed helpers (for the view)
include ApplicationHelper
# normal render statement
content = av.render template: 'widgets/index.xlsx.axlsx'
# do something with content, such as:"/tmp/with_runner.xlsx","w+b") {|f| f.puts content }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment