Ruby-on-rails 带有 Rails 3.1 的咖啡脚本中的 erb
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6504092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
erb in coffee script with rails 3.1
提问by denisjacquemin
I would like to use some erb in my .coffeefiles, like the following example
我想在我的.coffee文件中使用一些 erb,如下例所示
myLatlng: new google.maps.LatLng(<%[email protected] %>, <%[email protected] %>)
I renamed my locations.js.coffeeto locations.erb.coffee
我把我的改名locations.js.coffee为locations.erb.coffee
but I still get the following error
但我仍然收到以下错误
Error compiling asset application.js:
ExecJS::ProgramError: Error: Parse error on line 4: Unexpected 'COMPARE'
(in /Users/denisjacquemin/Documents/code/projects/geolog/app/assets/javascripts/locations.erb.coffee)
Served asset /application.js - 500 Internal Server Error
回答by Arcolye
If you want erb in the .coffeefiles IN YOUR VIEW folder, leave your file named as yourfilename.js.coffee, and Rails will still process the ERB, oddly enough.
如果你想要 erb 在.coffee你的 VIEW 文件夹中的文件,把你的文件命名为yourfilename.js.coffee,Rails 仍然会处理 ERB,奇怪的是。
To make it work in Heroku, move coffee-rails out of the assets group in your Gemfile.
要使其在 Heroku 中工作,请将 coffee-rails 移出 Gemfile 中的资产组。
回答by Clément
You may have to rename your file to locations.coffee.erb so erb is processed before coffee :)
您可能需要将文件重命名为 location.coffee.erb,以便在咖啡之前处理 erb :)
回答by David Beckwith
In Rails 3.2.8, I didn't have to move my .coffee file to /app/views. I just added .erb to the filename and left it in /app/assets/javascripts. Ie. I changed
在 Rails 3.2.8 中,我不必将我的 .coffee 文件移动到 /app/views。我只是在文件名中添加了 .erb 并将其留在 /app/assets/javascripts 中。IE。我变了
/app/assets/javascripts/user_answers.coffee.js to
/app/assets/javascripts/user_answers.coffee.js.erb
and then this worked:
然后这有效:
# Note the level of indentation.
var x = 2;
<% Question.first(2).each do |eq| %>
alert('eq: ' + <%= eq.id %>)
<% end %>
(The indentation level has to match in CoffeeScript, not Ruby.) Enjoy your coffee embedded in rubies.
(缩进级别必须在 CoffeeScript 中匹配,而不是在 Ruby 中。)享受嵌入红宝石的咖啡。
回答by Obromios
I agree with Ciro Centelli to leave the asset pipeline alone, especially if you are using Heroku. No doubt gonis useful if you need to many assignments, but you can also do this without a gem. In your html include
我同意 Ciro Centelli 不理会资产管道,尤其是在您使用 Heroku 的情况下。gon如果您需要完成许多任务,这无疑很有用,但您也可以在没有 gem 的情况下完成此操作。在你的 html 中包含
<%= javascript_tag do %>
window.latitude = <%[email protected] %>
window.longitdue = <%= @location.longitude %>
<% end %>
and in your coffee file
并在您的咖啡文件中
myLatlng: new google.maps.LatLng(window.latitude, window.longitude)
You can often work around other needs in a similar fashion. For instance if you do not want the coffee script to trigger on an element with particular id, then in the html use erb to only add that id when you want it triggered.
您通常可以以类似的方式解决其他需求。例如,如果您不希望咖啡脚本在具有特定 id 的元素上触发,则在 html 中使用 erb 仅在您希望触发时添加该 id。

