问题
原因是 Sprockets(Assets pineline 背后的宝石)不依赖于 Rails 来处理 erb。查看可用的本机助手https://github.com/sstephenson/sprockets#invoking-ruby-with-erb https://github.com/sstephenson/sprockets#invoking-ruby-with-erb
Rails 在 ActiveSupport 中为 Assets Pineline 添加了更多帮助程序,您可以使用它们。您可以在这里找到它们:http://api.rubyonrails.org/classes/ActionView/Helpers/AssetUrlHelper.html http://api.rubyonrails.org/classes/ActionView/Helpers/AssetUrlHelper.html
link_to
是一个属于ActionView的助手,所以它不包含在Assets Pineline中。
黑客
有一些解决方案允许您在 Assets Pineline 中使用 ActionView 助手:
资产管道中的路由助手 https://stackoverflow.com/questions/6725629/route-helpers-in-asset-pipeline
https://github.com/sstephenson/sprockets/issues/218 https://github.com/sstephenson/sprockets/issues/218
如何将 ActionView 助手包含在资产管道中? https://stackoverflow.com/questions/14284278/how-to-include-actionview-helpers-in-the-assets-pipeline
我的建议
如果您只需要有问题的链接或更多链接,则无需麻烦地破解。使用纯文本或 Javascript 帮助程序。这就够了。
//plain text
"<a href='#'>Sign up</a>"
//JS helper
Link = {}
Link.sign_up = "<a href='#'>Sign up</a>"
Link.link_to = (url, anchor) ->
"<a href=\"#{url}\">#{anchor}</a>"
console.log(Link.sign_up)
console.log(Link.link_to("#", "Sign up"))