0

I have not been able to understand why one should add extension .erb to an ordinary .js file in the app/assets folder? This is how I came to wonder this:

In my app, I want no more than one http request spent for a js file to be included in the body of the HTML page/DOM. This I can do by adding js filenames (with inclusion of lines like) into the application.js manifest file:

    //= require jquery
    //= require directory_name 
    //= require jQuery_plugins/carousel
    //= require jQuery_plugins/word-editor
    //= require jQuery_plugins/abc_something

and so on.

Now I have a long list of javascripts for the entire rails app, and I want the manifest file to output (to include) javascripts in a page-specific way. To manage this, I attempted to add conditions like:

    if params[:controller] == "my_controller" && params[:action] == "index"
      //= require jQuery_plugins/carousel

Also I converted application.js to application.js.erb file to see if that worked. But no! It did not work. Manifest will output everything that comes its way. Looks like a question was posed for a similar problem on SOF, but solutions given in the answers there seem very old-school and rather unclean traditional way.

Where-do-you-put-your-page-specific-javascript-code

So what is, really, the use of converting js into js.erb file? And can I manage page-specific output purely on the basis a "js manifest" file?

Community
  • 1
  • 1
Marvin Danig
  • 3,738
  • 6
  • 39
  • 71

2 Answers2

1

I use it for things such as:

if (<%=j @tarea.errors[:texto].any?.to_s%>){
    $('#texto_tooltip').html("<%= @tarea.errors[:texto].first.to_s %>");
    $('#texto_field').addClass("error-box");
}

depending on object state I can execute some jquery or another

Perseoh
  • 612
  • 8
  • 13
1

So, you have .js.erb so you can embed ruby code in your js, which will be executed and output js into the middle of the file. Maybe that answer is too obvious.

One reason you might want to do this is to use the asset_path helper to embed the path to another one of the application's assets in a js string literal. I think you can also use url_for or other route helpers to, say, embed the URL to a particular application action in a JS string literal too. But I haven't tried that, not every usual Rails helper method is available in asset ERBs, only a subset, and they don't always work quite exactly like the usual helpers.

What you can not do is what you were originally proposing, embed ruby that is conditional on the current request or current page. This is because assets in the asset pipeline are not compiled (ie, executed) for every request (at least not ordinarily in production), they are instead only compiled once for the life of the application and cached -- so at the time they are compiled and cached, there is no 'current' request or page or request params.

In general, .js.erb probably isn't used too often, it's there just for consistency and in case you need it. There are probably other odd cases where it would be convenient to have ruby code generating JS code in an asset, even without using url helper methods. So why not make it possible, since the asset pipeline means assets are going through some processing/execution anyway, it's just kind of natural to let you send em through ERB too if you want.

jrochkind
  • 22,799
  • 12
  • 59
  • 74