Skip to content

Instantly share code, notes, and snippets.

@tastycode
Created December 10, 2014 23:37
Show Gist options
  • Save tastycode/daa84c0bbdb8b2c3a63a to your computer and use it in GitHub Desktop.
Save tastycode/daa84c0bbdb8b2c3a63a to your computer and use it in GitHub Desktop.
Render rails templates from javascript handlebars templates
# config/initializers/sprockets.rb
module Sprockets::Helpers::RailsHelper
def view_renderer
@view_renderer ||= begin
view_paths = Rails::Application::Configuration.new(Rails.root).paths["app/views"]
ActionView::Base.new(view_paths).tap do |renderer|
def renderer.method_missing(name, *args)
router = Rails.application.routes.url_helpers
return router.send(name, *args) if router.respond_to?(name)
super(name, *args)
end
end
end
end
def render(*args)
return super if defined?(super)
view_renderer.render(*args)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment