在rails asset pipeline中的js.coffee文件中使用erb时出错 [英] Error using erb in js.coffee files in the rails asset pipeline

查看:124
本文介绍了在rails asset pipeline中的js.coffee文件中使用erb时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:



/assets/javascripts/home.js.coffee.erb

  jQuery  - > 
addClickListeners = - >
$(document).on'click','#add-chord-link',addChord
$(document).on'click','#remove-chord-link',removeChord

addChord =(e) - >
e.preventDefault()
console.log(<%= asset_path('rails.png')%>)
console.log(<%= link_to'立即注册!','#'%>)
console.log('addChord clicked')
$('#chord-choices')。append('addedChord')

removeChord =(e) - >
e.preventDefault()
$('#chord-choices select')。last()。remove()
console.log('removeChord clicked')

addClickListeners()

console.log <%= asset_path('rails.png')%>) /assets/rails.png 。但是,每当我包含 console.log(<%= link_to'立即注册!','#'%>)

 未定义的方法`link_to'for#<#< Class:0x007f9095960938>:0x007f9095b78ab8& 

为什么这不工作?

解决方案

问题



原因是Sprockets是Assets pineline背后的宝石,不依赖于Rails来处理erb。 查看可用的本机帮手 https://github.com/sstephenson/sprockets #invoking-ruby-with-erb



Rails在ActiveSupport中为资产Pineline添加了更多帮助,他们都是您可以使用的。您可以在这里找到它们: http://api.rubyonrails.org/classes/ActionView /Helpers/AssetUrlHelper.html



link_to 是属于ActionView的帮手,因此不包括资产小队。



hacks



有一些解决方案允许您在资产Pineline中使用ActionView助手:



资产管道中的路由助手

https://github.com/sstephenson/ sprockets / issues / 218



如何在资产管道中包含ActionView助手?



我的建议



如果你需要的是有问题的链接或多一点,没有必要麻烦了。使用纯文本或Javascript帮助器。就够了。

  //纯文本
< a href ='#'>注册& a>

// JS helper
Link = {}
Link.sign_up =< a href ='#'>注册< / a&
Link.link_to =(url,anchor) - >
< a href = \#{url} \>#{anchor}< / a>

console.log(Link.sign_up)
console.log(Link.link_to(#,注册))


I have the following code:

/assets/javascripts/home.js.coffee.erb

jQuery ->
  addClickListeners = ->
        $(document).on 'click', '#add-chord-link', addChord
        $(document).on 'click', '#remove-chord-link', removeChord

    addChord = (e) ->
        e.preventDefault()
        console.log("<%= asset_path('rails.png') %>")
        console.log("<%= link_to 'Sign up now!', '#' %>")
        console.log('addChord clicked')
        $('#chord-choices').append('addedChord')

    removeChord = (e) ->
        e.preventDefault()
        $('#chord-choices select').last().remove()
        console.log('removeChord clicked')

    addClickListeners()

The console output for the console.log("<%= asset_path('rails.png') %>") is /assets/rails.png, which is what I expect. However, whenever I include console.log("<%= link_to 'Sign up now!', '#' %>") I get an error when the page is loaded stating:

    undefined method `link_to' for #<#<Class:0x007f9095960938>:0x007f9095b78ab8>

Why is this not working?

解决方案

The problem

The reason is Sprockets, the gem behind Assets pineline, doesn't depend on Rails to process erb. See the native helpers available https://github.com/sstephenson/sprockets#invoking-ruby-with-erb

Rails added some more helpers to Assets Pineline in ActiveSupport, they are all you can use. You can find them here: http://api.rubyonrails.org/classes/ActionView/Helpers/AssetUrlHelper.html

link_to is a helper belonging to ActionView, so it's not included in Assets Pineline.

The hacks

There are some solutions to allow your using ActionView helpers within Assets Pineline:

Route helpers in asset pipeline

https://github.com/sstephenson/sprockets/issues/218

How to include ActionView helpers in the assets pipeline?

My suggestions

If all you need is the link in question or a little bit more, no need the trouble to hack around. Use plain text or a Javascript helper. That's enough.

//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"))

这篇关于在rails asset pipeline中的js.coffee文件中使用erb时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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