Rails应用到角:HAML + Rails的助手 [英] Rails App to Angular: HAML + Rails Helpers

查看:219
本文介绍了Rails应用到角:HAML + Rails的助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在同一时间的应用程序在移动我的全栈轨角的页面。我使用的UI路由器( https://github.com/angular-ui/ui-router )和角护栏模板( https://github.com/pitr/angular -rails-模板)。我以为nghaml扩展可以让我继续使用Rails的helper如的link_to,路径等我HAML所以只是复制和粘贴我的H​​AML页到模板;在一个理想的世界,我现在是在其中一个网页被所服务的客户端和所有其他网页(包括它链接到的)仍在担任服务器端的一个点。相反,我得到错误,例如:

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 (https://github.com/pitr/angular-rails-templates). I assumed the nghaml extension would allow me to continue to use rails helpers such as link_to, paths, etc. in my haml so just copied and pasted my haml page into the template; in an ideal world I would now be at a point where one page was being served client-side and every other page (including the ones it's linked to) were still being served server-side. Instead, I'm getting errors such as:

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

和的link_to等。

and link_to, etc.

我认为这( angularjs与客户端HAML )将是一个固溶体,特别是sharpper的反应,因为它似乎直接适用。

I thought this (angularjs with client side haml) would be a solid solution, specifically sharpper's response since it seemed directly applicable.

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, no dice.

思考?

推荐答案

陷同样的问题和调查的角轨,模板,聚精会神地阅读他们的文档和使用的 angularjs与客户端HAML

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.

角轨的模板需要重新创建HAML发动机mimeless版本。所以它们延伸是与倾斜代替注册用加入到该资产的管道,该发动机的类。因此,我们创造并与资产管道注册新的CustomHamlEngine从未使用角轨模板。我们需要做的,而不是什么是注册发动机的倾斜

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.

创建一个名为 angular_rails_templates.rb 配置/初始化文件夹,并把这个code在里面。

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'

这将覆盖与我们刚刚创建的一个平常.haml引擎。然后角轨模板将处理您的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.

这篇关于Rails应用到角:HAML + Rails的助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆