erb 在带有 Rails 3.1 的咖啡脚本中

发布于 2024-11-17 12:09:52 字数 835 浏览 2 评论 0原文

我想在我的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沉默的熊 2024-11-24 12:09:52

如果您希望将 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 as yourfilename.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.

雨夜星沙 2024-11-24 12:09:52

您可能需要将文件重命名为locations.coffee.erb,以便在咖啡之前处理erb:)

You may have to rename your file to locations.coffee.erb so erb is processed before coffee :)

白衬杉格子梦 2024-11-24 12:09:52

在 Rails 4 中尽可能坚持使用资源管道,而不是使用 js.erb 视图。

使用 gon 或其他一些技术将变量传递给 Js:Ruby on Rails - 将 JavaScript 变量从控制器发送到外部 Javascript 资源文件

使用 gon

app/views/layouts/application.html.erb:

<head>
  <meta charset="utf-8"/>
  <%= include_gon %>

app/controllers/application_controller.rb:

before_filter do
  gon.latitude = 0.1
  gon.longitude = 0.2
end

app/assets/javascripts/locations.js.coffee:

myLatlng: new google.maps.LatLng(gon.latitude, gon.longitude)

此方法速度更快,因为文件仅在启动时预编译一次,由服务器而不是通过 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:

<head>
  <meta charset="utf-8"/>
  <%= include_gon %>

app/controllers/application_controller.rb:

before_filter do
  gon.latitude = 0.1
  gon.longitude = 0.2
end

app/assets/javascripts/locations.js.coffee:

myLatlng: new google.maps.LatLng(gon.latitude, gon.longitude)

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.

緦唸λ蓇 2024-11-24 12:09:52

在 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

,然后这个工作了:(

# Note the level of indentation.
var x = 2;

<% Question.first(2).each do |eq| %>
alert('eq: ' + <%= eq.id %>)
<% end %>

缩进级别必须与 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

/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.

一绘本一梦想 2024-11-24 12:09:52

我同意 Ciro Centelli 的观点,不要管资产管道,特别是如果您使用 Heroku。毫无疑问,如果您需要进行许多作业,gon 会很有用,但您也可以在没有 gem 的情况下完成此操作。在您的 html 包含文件

<%= javascript_tag do %>
    window.latitude = <%[email protected] %>
    window.longitdue = <%= @location.longitude %>
<% end %>

和咖啡文件

myLatlng: new google.maps.LatLng(window.latitude, window.longitude)

中,您通常可以以类似的方式解决其他需求。例如,如果您不希望咖啡脚本在具有特定 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 include

<%= 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文