In my rails 3.1.3 app, I'd like to insert some ERB code in my javascript file but it's not being parsed for some reason:
# app/assets/javascripts/application.js
//= require_tree ./shared
# app/assets/javascripts/shared/shared.js.erb
MM.loading = '<img src="<%= asset_path("icons/ajax-loader.gif") >">';
Gets rendered like this in /application.js
:
MM.loading = '<img src=" asset_path("icons/ajax-loader.gif") >">';
I can't see any extra steps in the rails guides - is there something I'm missing? Btw I'm using haml for the view files, and also tried the above with .js.haml
, enclosing in #{...}
.
You have a syntax error in your code. This:
MM.loading = '<img src="<%= asset_path("icons/ajax-loader.gif") >">';
should be this:
MM.loading = '<img src="<%= asset_path("icons/ajax-loader.gif") %>">';
You were missing the closing erb tag for the helper block of code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With