Rails app for Angular: HAML + Rails helpers

I am trying to move my full stack application to an Angular page at a time. I use ui-router ( https://github.com/angular-ui/ui-router ) and angular -rails-templates ( https://github.com/pitr/angular-rails-templates ). I suggested that the nghaml extension would allow me to continue to use rail assistants such as link_to, paths, etc. In my haml, so I just copied and pasted my haml page into a template; in an ideal world, I would now be at the point where one page was served on the client side, and every other page (including those with which it was linked) was still served on the server side. Instead, I get errors such as:

undefined local variable or method `dashboard_patients_path' for #<Object:0x007fc87865cff0> 

link_to etc.

I thought this ( angularjs from the client side of haml ) would be a reliable solution, especially a sharp answer, as it seems straightforward.

 module CustomHamlEngine class HamlTemplate < Tilt::HamlTemplate def evaluate(scope, locals, &block) scope.class_eval do include Rails.application.routes.url_helpers include Rails.application.routes.mounted_helpers include ActionView::Helpers end super end end end Rails.application.assets.register_engine '.haml', CustomHamlEngine::HamlTemplate 

However, even after restarting the server, there are no dice.

Thoughts?

+1
source share
1 answer

I got into the same problem and found a solution after examining angular-rails-templates , carefully reading their documentation and using the solution proposed by the cutter in angularjs with the client side haml .

angular -rails-templates needs to recreate a homeless version of the haml engine. Therefore, they extend classes registered with Tilt, instead of using engines that have been added to asset pipelines. Therefore, the new CustomHamlEngine that we create and register in the asset pipeline is never used by the angular -rails template. Instead, we need to register a sloped engine.

Create the angular_rails_templates.rb file in the config / initializers folder and paste this code into it.

 # config/initializers/angular_rails_templates.rb module CustomHamlEngine class HamlTemplate < Tilt::HamlTemplate def evaluate(scope, locals, &block) scope.class_eval do include Rails.application.routes.url_helpers include Rails.application.routes.mounted_helpers include ActionView::Helpers end super end end end Tilt.register CustomHamlEngine::HamlTemplate, '.haml' 

This will replace the regular .haml engine with the one we just created. angular -rails-templates will process your haml file and it will support rail assistants as well as path helpers.

Remember to restart the server after including the file.

+6
source

All Articles