Rails App to Angular: HAML + Rails Helpers

前端 未结 1 629
忘了有多久
忘了有多久 2021-01-07 10:04

I\'m trying to move my full-stack rails app over to Angular a page at a time. I\'m using ui-router (https://github.com/angular-ui/ui-router) and angular-rails-templates (htt

相关标签:
1条回答
  • 2021-01-07 10:55

    Got stuck with the same problem and found a solution after investigating angular-rails-templates, attentively reading their documentation and using the solution proposed by sharpper in angularjs with client side haml.

    angular-rails-templates needs to recreate a mimeless version of the haml engine. So they extend the classes that are registered with Tilt instead of using the engines that were added to the asset pipelines. Therefore the new CustomHamlEngine that we create and register with the asset pipeline is never used by angular-rails-template. What we need to do instead is to register the engine with Tilt.

    Create a file called angular_rails_templates.rb in the config/initializers folder and put this code in 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'
    

    That will override the usual .haml engine with the one we just created. Angular-rails-templates will then process your haml file and it will support the rails helpers as well as the path helpers.

    Don't forget to restart the server after including the file.

    0 讨论(0)
提交回复
热议问题