Rails 3.1 asset pipeline: how to load controller-specific scripts?

匿名 (未验证) 提交于 2019-12-03 02:05:01

问题:

If I generate a new controller in Rails 3.1, also a javascript file with the name of the controller will added automatically. Firstly, I thought this javascript file will used only, when the related controller is called.

By default there is the instruction //= require_tree . in the application.js-file, that include every javascript file on it's tree.

How could I load only the controller specific script?

回答1:

To load only the necessary name_of_the_js_file.js file:

  1. remove the //=require_tree from application.js

  2. keep your js file (that you want to load when a specific page is loaded) in the asset pipeline

  3. add a helper in application_helper.rb

    def javascript(*files)   content_for(:head) { javascript_include_tag(*files) } end 
  4. yield into your layout:

     
  5. add this in your view file:

     

Then it should be ok



回答2:

An elegant solution for this is to require controller_name in your javascript_include_tag

see http://apidock.com/rails/ActionController/Metal/controller_name/class

 

controller_name.js will be loaded and is in the asset also, so you can require other files from here.

Example, rendering cars#index will give

 

where cars.js can contain

//= require wheel //= require tyre 

Enjoy !



回答3:

I always include this inside my layout files. It can scope your js to action

  


回答4:

Your problem can be solved in different ways.

Add the assets dynamically

Please consider that this isn't a good solution for the production mode, because your controller specifics won't be precompiled!

  1. Add to our application helper the following method:

    module ApplicationHelper     def include_related_asset(asset)     #          v-----{Change this}         if !YourApp::Application.assets.find_asset(asset).nil?             case asset.split('.')[-1]                 when 'js'                     javascript_include_tag asset                 when 'css'                     stylesheet_link_tag asset             end         end     end end 
  2. Call the helper method in your layout-file:

     
  3. Create specific assets for your controller actions. E. g. controller_action.js

Please don't forget to change YourApp to the name of your app.

Use yield

  1. Add to your layout head
  2. Include your assets from your action views:

       

Please see the Rails guides for further information.



回答5:

Another option is to use pluggable_js gem.



回答6:

I like albandiguer's solution. With which I've found that javascript/coffeescript assets are not individually precompiled. Which causes all sorts of errors trying to use javascript_path. I'll share my solution to that problem after I address an issue a few people mentioned in his comments. Mainly dealing with only a partial set of controller named JavaScript files.

So I built an application helper to detect if the file exists in the javascript directory regardless of .coffee/.js extension:

module ApplicationHelper   def javascript_asset_path(basename)     Sprockets::Rails::Helper.assets.paths.select{|i|       i =~ /javascript/ and i =~ /#{Rails.root}/     }.each do |directory|       if Dir.entries(directory).map {|i| i.split('.')[0]}.compact.           include? basename         return File.join(directory, basename)       end     end     nil   end end 

This method will return the full path to the javascript file if it exists. Otherwise it returns nil. So following Pencilcheck's comment you can add this method for a conditional include:

 

And now you have a proper conditional include. Now for the issue of precompiled assets. Generally for optimization you don't want assets precompiled individually. You can however do it if you must:

# Live Compilation config.assets.compile = true 

You can add this do your environment config file. Test it in your development environment file first. Again this is ill-advisable. The Rails asset pipeline uses Sprockets to optimize everything:

Sprockets loads the files specified, processes them if necessary, concatenates them into one single file and then compresses them (if Rails.application.config.assets.compress is true). By serving one file rather than many, the load time of pages can be greatly reduced because the browser makes fewer requests. Compression also reduces file size, enabling the browser to download them faster.

PLEASE READ the documentation for further details of the mechanics of Sprockets (Asset Pipeline) http://guides.rubyonrails.org/asset_pipeline.html

Assets aren't precompiled individually. For example when I try:

 

I get:

Sprockets::Rails::Helper::AssetFilteredError: Asset filtered out and will not be served: add Rails.application.config.assets.precompile += %w( event.js ) to config/initializers/assets.rb and restart your server

So you can include which assets to be precompiled individually. We just need to add the relevant controller named javascript files in our asset initializer. Well we can do this programatically.

To get a list of controller names I will use ecoologic's example:

all_controllers =  Dir[     Rails.root.join('app/controllers/*_controller.rb')   ].map { |path|     path.match(/(\w+)_controller.rb/); $1   }.compact 

And now to get the name of all javascript files that match the basename of the controller name you can use the following:

javascripts_of_controllers = Sprockets::Rails::Helper.assets.paths.select{|a_path|     a_path =~ /javascript/ and a_path =~ /#{Rails.root}/   }.map {|a_path|     Dir.entries(a_path)   }.flatten.delete_if {|the_file|     !the_file['.js']   }.collect {|the_file|     the_file if all_controllers.any? {|a_controller| the_file[a_controller]}   } 

Then you can try:

# config/initializers/assets.rb Rails.application.config.assets.precompile += javascripts_of_controllers 

This will get you a list of all javascript files, without directory path, that match your controller name. Note if your controller name is plural, the javascript name should be as well. Also note if the controller is singular and the javascript file is plural this will still include it because of the_file[a_controller] will succeed on a partial match.

Feel free to try this out in your Rails.application.config.assets.precompile setting. I know that this gets you the list of files correctly. But I'll leave you to test it. Let me know if there are any nuances involved with precompiling this way as I am curious.

For a very thorough explanation on how assets precompile see this blog: http://www.sitepoint.com/asset-precompile-works-part/



回答7:

I recently found a simple approach to use generated scripts for specific controller. I use for that solution gem gon. Add in a controller:

class HomesController 

After that open your homes.js.cofee and add in the beginning of file:

jQuery ->   if gon.controller == "sermons"     # Place all functions here... 

That is all.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!