erb 在带有 Rails 3.1 的咖啡脚本中
我想在我的 .coffee
文件中使用一些 erb,如下例所示,
myLatlng: new google.maps.LatLng(<%[email protected] %>, <%[email protected] %>)
我将我的 locations.js.coffee
重命名为 locations.erb.coffee
但我仍然收到以下错误
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
I would like to use some erb in my .coffee
files, like the following example
myLatlng: new google.maps.LatLng(<%[email protected] %>, <%[email protected] %>)
I renamed my locations.js.coffee
to 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您希望将 erb 放在 YOUR VIEW 文件夹中的 .coffee 文件中,请将文件命名为 yourfilename.js.coffee,Rails 仍然会处理 ERB,这很奇怪。
要使其在 Heroku 中工作,请将 Coffee-rails 从 Gemfile 中的资产组中移出。
If you want erb in the
.coffee
files IN YOUR VIEW folder, leave your file named asyourfilename.js.coffee
, and Rails will still process the ERB, oddly enough.To make it work in Heroku, move coffee-rails out of the assets group in your Gemfile.
您可能需要将文件重命名为locations.coffee.erb,以便在咖啡之前处理erb:)
You may have to rename your file to locations.coffee.erb so erb is processed before coffee :)
在 Rails 4 中尽可能坚持使用资源管道,而不是使用
js.erb
视图。使用 gon 或其他一些技术将变量传递给 Js:Ruby on Rails - 将 JavaScript 变量从控制器发送到外部 Javascript 资源文件
使用
gon
:app/views/layouts/application.html.erb:
app/controllers/application_controller.rb:
app/assets/javascripts/locations.js.coffee:
此方法速度更快,因为文件仅在启动时预编译一次,由服务器而不是通过 Rails 提供服务,并且与相同的 HTTP 请求相同其余的Js。
Stick to the asset pipeline when possible in Rails 4, instead of using a
js.erb
view.Pass variables to the Js using gon or some other technique discussed at: Ruby on Rails - Send JavaScript variable from controller to external Javascript asset file
With
gon
:app/views/layouts/application.html.erb:
app/controllers/application_controller.rb:
app/assets/javascripts/locations.js.coffee:
This method is faster because file is precompiled only once at startup, gets served by the server instead of through Rails, and on the same HTTP request as the rest of the Js.
在 Rails 3.2.8 中,我不必将 .coffee 文件移至 /app/views。我刚刚将 .erb 添加到文件名中,并将其保留在 /app/assets/javascripts 中。 IE。我改变了
,然后这个工作了:(
缩进级别必须与 CoffeeScript 匹配,而不是 Ruby。)享受嵌入红宝石的咖啡。
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
and then this worked:
(The indentation level has to match in CoffeeScript, not Ruby.) Enjoy your coffee embedded in rubies.
我同意 Ciro Centelli 的观点,不要管资产管道,特别是如果您使用 Heroku。毫无疑问,如果您需要进行许多作业,
gon
会很有用,但您也可以在没有 gem 的情况下完成此操作。在您的 html 包含文件和咖啡文件
中,您通常可以以类似的方式解决其他需求。例如,如果您不希望咖啡脚本在具有特定 id 的元素上触发,则在 html 中使用 erb 仅在您希望触发时添加该 id。
I agree with Ciro Centelli to leave the asset pipeline alone, especially if you are using Heroku. No doubt
gon
is useful if you need to many assignments, but you can also do this without a gem. In your html includeand in your coffee file
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.