我正在开发 Rails 3.2 + AngularJS 应用程序,并将所有 AngularJS 代码放在名为“api_path”的 Rails 路由下(例如:/api#)
在我的 AngularJS 模板中设置指向 CRUD 函数(例如“show”)的链接时,我一直在执行以下操作;
<td><a href="/api#/costings/{{costing.id}}">Show</td>
我的 AngularJS 模板位于 Assets 管道中,名为 show.html.erb。我知道它正在正确编译 erb,因为我可以执行 <%= 2 + 2 %> 并且可以正常工作。
我的 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>
最佳答案
- 我认为在 AngularJS 中使用助手不是一个好主意。
assets/*
中的模板无法共享applicationHelper
范围,因此无法执行此操作。- 如果必须,请尝试在
views/*
中使用 erb,它应该可以工作。
关于javascript - 在 AngularJS 模板中使用 Rails 助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22801749/