1

I am using Rails 3.2.1 where I have to include few controller and action specific Javascripts. For which I am using a _javascript_template_manager.html.haml and calling this at the bottom of the page. Now I want to combine all these js into one to reduce the http request. I am using asset pipeline.

How can I combine controller specific js into one precompiled file?

Here is how my _javascript_template_manager.html.haml file looks like

 %noscript
  :css
    #container{ display:none; }
  = render :partial => "/widgets/common/javascript_disabled"


= jquery_include_tag :google 
= javascript_include_tag "common"

/ condetional javascripts for the app
- if params[:controller] == "settings"
  = javascript_include_tag "plugins/jQuery-cookie"

- if params[:controller] != "people" and params[:controller] != "companies"
  = javascript_include_tag "plugins/iphone-style-checkboxes"

- if params[:controller] == 'users' || params[:controller] == 'companies'
  = javascript_include_tag "plugins/best_in_place"

- if params[:controller] == 'letters' || params[:controller] == 'companies'
  = javascript_include_tag "plugins/jquery.autoSuggest"

- if params[:controller] == 'people' || params[:controller] == 'letters' || params[:controller] == 'users' || params[:controller] == 'companies'
  = javascript_include_tag "plugins/areacomplete"

= javascript_include_tag "application"

Now I want to combine all the above javascript files into one or what is the best way to do it?

3

1 Answer 1

0

Firstly, read about controller specific assets. Then you have to require all necessary files in your controller specific JS file. For example, in app/assets/javascripts/settings.js you would add:

//= require plugins/jQuery-cookie

After setting this all up, you don't have to worry about too many requests, because the asset pipeline will automatically compress everything to one JS file.

2
  • 1
    Thanks, So you mean to say that I have to include //= require plugins/jQuery-cookie in every controller specific js if it is required in multiple controllers? And what about controller and action conditions combined? Documentation does not talk about 'action' specific js at all. Commented Jul 30, 2013 at 13:59
  • You may ask yourself a question if you really have to use a controller specific js. As long as these plugins don't clash with eachother, they can be just included in application.js. You can read more about controller and action specific js here. Commented Jul 30, 2013 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.