【问题标题】:using rails helpers in angularjs templates在 angularjs 模板中使用 rails 助手
【发布时间】:2014-04-02 04:42:40
【问题描述】:

我正在开发 Rails 3.2 + AngularJS 应用程序,并将我所有的 angularJS 代码放在名为“api_path”(EG:/api#)的 rails 路由下

在我的 AngularJS 模板中设置指向 CRUD 函数(例如“显示”)的链接时,我一直在执行以下操作;

<td><a href="/api#/costings/{{costing.id}}">Show</td>

我的 AngularJS 模板在资产管道中并命名为 show.html.erb。我知道它正在正确编译 erb,因为我可以做到 并且有效。

我的 application_helper.rb 文件中有这样的方法;

  def link_to_api(title, path)
    link_to title, api_path + "#" + path
  end

如果我尝试在我的 AngularJS 模板中使用它,我会收到错误:

NoMethodError at /costings/index.html.erb
=========================================

> undefined method `link_to_api' for #<#<Class:0x007f4e8f550340>:0x007f4e9391ab20>
  (in app/assets/templates/costings/index.html.erb)

app/assets/templates/costings/index.html.erb, line 8

这是我在模板中尝试过的

<td><%= link_to_api("Show", "costings/{{costing.id}}") %></td>

【问题讨论】:

    标签: javascript ruby-on-rails ruby-on-rails-3 angularjs


    【解决方案1】:
    1. 我认为在 AngularJS 中使用帮助器不是一个好主意。
    2. assets/* 中的模板无法共享applicationHelper 范围,因此无法执行此操作。
    3. 如果必须,请尝试在 views/* 中使用 erb,它应该可以工作。

    【讨论】:

    • 公平点。那么有没有比 'Show' 更好的方法来设置我的路径?
    • api_path 作为 angular-constants ,然后尝试 ui-router。并将标题和路径放在范围内....我不确定您的要求。
    猜你喜欢
    • 1970-01-01
    • 2014-05-12
    • 1970-01-01
    • 2011-10-22
    • 2013-06-19
    • 2014-06-20
    • 2014-08-12
    • 1970-01-01
    • 2015-01-15
    相关资源
    最近更新 更多