Doing slim templates with angular & rails? Try this.
If you have a directory in your asset pipeline that you want to compile the templates and use them with your angular routes, you can do that using a setup like this.
# app/assets/javascripts/app.js.coffee.erb
window.app = angular.module "website", []
app.config ($routeProvider, $locationProvider) ->
$locationProvider.html5Mode(true)
$routeProvider
.when "/",
templateUrl: "<%= asset_path("welcome.html") %>"
# config/initializers/slim_assets.rb
Rails.application.assets.register_engine('.slim', Slim::Template)
# config/initializers/asset_helpers.rb
Rails.application.assets.context_class.class_eval do
include ActionView::Helpers
include Rails.application.routes.url_helpers
end
# app/assets/template/welcome.html.slim
h3 Welcome!
= image_tag asset_path("logo.png")Written by Tim Anema
Related protips
5 Responses
Would be super interested to see if you could have a follow up tip for testing directives that use slim templates in rails. Nice tip btw. :D
I have addressed this in another post now https://coderwall.com/p/ddpara?i=1&p=1&q=author%3Atanema&t%5B%5D=tanema Let me know what you think.
Awesome tip! One thing that tripped me up is that I had to change:
= img_tag asset_path("logo.png")
to
= image_tag asset_path("logo.png")One more thing: I also had to rename the coffee file:
app/assets/javascripts/app.js.coffee
to
app/assets/javascripts/app.js.coffee.erbAh yes those were my mistakes I have made the changes to the post. Thanks!
Have a fresh tip? Share with Coderwall community!
AngularJS pages are long-lived browser apps with the same script risk as a newer single-page app. Third-party code in the template can read forms and values rendered on the page.
Security insights from Cside