Skip to content

Instantly share code, notes, and snippets.

@straydogstudio
Last active August 29, 2015 14:05
Show Gist options
  • Save straydogstudio/dceb775ead81470cea70 to your computer and use it in GitHub Desktop.
Save straydogstudio/dceb775ead81470cea70 to your computer and use it in GitHub Desktop.
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'
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: 'db/development.sqlite3'
)
ActionController::Base.prepend_view_path "./app/views/"
# place data in view_assigns
view_assigns = {widgets: Widget.all}
av = ActionView::Base.new(ActionController::Base.view_paths, view_assigns)
av.class_eval do
# include any needed helpers (for the view)
include ApplicationHelper
end
# normal render statement
content = av.render template: 'widgets/index.xlsx.axlsx'
# do something with content, such as:
File.open("/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