Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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