Jamesla
Jamesla

Reputation: 1408

Javascript won't load with .js.erb extension

I have a javascript file in my rails site which loads fine with the extension .js.

I need to use an image helper tag in it, so according to the documentation I need to add an erb extension to - making it .js.erb.

As soon as I change the extension to .js.erb (leaving the javascript file as is, IE no ruby) it breaks and the javascript no longer works.

When I check it with chrome inspector the javascript contents are as follows:

(function() {


}).call(this);

However it is fine when the extension is just .js.

What am I doing wrong?

edit:: without the .erb extension the relevent javascript line is returning this in chrome inspector:

function fancyViewReport() {
    return '<div class="demo-report-title"><a class="fancyReport" href="<%= asset_path 'demoreport.png' %>">Click <u>here</u> to view a demo report!</a></div>';
}

this javascript file is being called through application.js

Upvotes: 0

Views: 892

Answers (2)

Jamesla
Jamesla

Reputation: 1408

Ok i eventually fixed this by creating a new file and copy pasting the contents of the previous javascript file into it, pretty strange.

Upvotes: 0

epsilones
epsilones

Reputation: 11609

As far as I know, js.erb are made for ajax type requests. I think that if your ajax_method called is under controller someController and if you have someController.js.erb file, you should put it in app/views/someController and make sure you have somethig like that

def ajax_method
    ...
    respond_to do |format|
      format.js
    end
  end

Upvotes: 1

Related Questions