Skip to content

Instantly share code, notes, and snippets.

@mraaroncruz
Last active December 21, 2015 12:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mraaroncruz/6305328 to your computer and use it in GitHub Desktop.
Save mraaroncruz/6305328 to your computer and use it in GitHub Desktop.
A rails controller to deal with angular templates.
# Just put your erb, haml, slim, etc. templates in app/views/angular_templates/my_template_name.
# For example your templatePath could be `articles/show` and your template
# would be app/views/angular_templates/articles/show.html.erb
class AngularTemplatesController < ApplicationController
def show
path = params[:template_path]
template_dir = File.join(Rails.root, "app", "views", "angular_templates")
templates = Dir.glob("#{template_dir}/**/*").map { |file|
file.sub(/.*?\/angular_templates\/(.+)\.html\.slim/, '\1')
}
if templates.include?(path)
render "angular_templates/#{path}", layout: false
else
render status: :unprocessible_entity, text: "ERROR"
end
end
end
MyApp::Application.routes.draw do
get "angular_templates/*template_path", to: "angular_templates#show"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment