erb en coffee script con rails 3.1


Me gustaría usar algunos erb en mis archivos .coffee, como el siguiente ejemplo

myLatlng: new google.maps.LatLng(<%[email protected] %>, <%[email protected] %>)

Cambié el nombre de mi locations.js.coffee a locations.erb.coffee

Pero todavía tengo el siguiente 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
Author: denisjacquemin, 2011-06-28

5 answers

Si desea erb en los archivos .coffee EN SU carpeta VIEW, deje su archivo con el nombre yourfilename.js.coffee, y Rails seguirá procesando el ERB, por extraño que parezca.

Para que funcione en Heroku, mueva coffee-rails fuera del grupo assets en su Gemfile.

 75
Author: Arcolye,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-09 01:14:43

Es posible que tenga que cambiar el nombre de su archivo a ubicaciones.café.erb así erb se procesa antes del café:)

 13
Author: Clément,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-28 10:11:33

En Rails 3.2.8, no tenía que mover mi.archivo de café a/app / views. Acabo de añadir .erb al nombre del archivo y lo dejó en / app / assets / javascripts. IE. He cambiado

/app/assets/javascripts/user_answers.coffee.js to 
/app/assets/javascripts/user_answers.coffee.js.erb

Y entonces esto funcionó:

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

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

(El nivel de sangría tiene que coincidir en CoffeeScript, no en Ruby.) Disfrute de su café incrustado en rubíes.

 4
Author: David Beckwith,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-05 14:48:37

Apégate a la canalización de activos cuando sea posible en Rails 4, en lugar de usar una vista js.erb.

Pasar variables al Js usando gon o alguna otra técnica discutida en: Ruby on Rails-Enviar variable JavaScript desde el controlador al archivo externo de activos Javascript

Con 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.café:

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

Este método es más rápido porque el archivo se precompila solo una vez al inicio, se sirve por el servidor en lugar de a través de Rails, y en la misma solicitud HTTP que el resto del Js.

 4
Author: Ciro Santilli 新疆改造中心 六四事件 法轮功,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 11:54:02

Estoy de acuerdo con Ciro Centelli para dejar la tubería de activos en paz, especialmente si está utilizando Heroku. Sin duda gon es útil si necesitas muchas tareas, pero también puedes hacerlo sin una gema. En su html incluir

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

Y en su archivo de café

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

A menudo se puede trabajar alrededor de otras necesidades de una manera similar. Por ejemplo, si no desea que el script coffee se active en un elemento con un id particular, entonces en el html use erb para agregar ese id solo cuando desee se disparó.

 1
Author: Obromios,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-15 16:36:37