Rails 3.1 and Higher: Load Controller Specific Scripts (css, js)

Rails has a feature of loading scripts automatically when included in application.js or css like

//= require jquery

//= require jquery_ujs

# code below in the application.js file tells to load every script in the tree.

//= require_tree

Now for common scripts you can use above method but, if you want to load controller specific scripts the you can follow the steps below:

  1. Add controller_name in the following line in file views/layout/application.html.erb

    <%= javascript_include_tag ‘application’, controller_name %>

  2. Add the following line in /config/initilizers/assets.rb to precompile additional assets.

    Rails.application.config.assets.precompile += %w( bids.js )

  3. After Changing settings inside any file within config folder, you need to restart the server.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s