Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding erb code in javascript for asset pipeline

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 #{...}.

like image 786
Zubin Avatar asked Jan 23 '12 00:01

Zubin


1 Answers

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.

like image 53
Richard Hulse Avatar answered Sep 20 '22 15:09

Richard Hulse