开发者

Rails 3.1 strategy for pre-compiling controller specific JS assets

In order to keep controller specific JavaScript logic out of the standard application.js and only have it included by the relevant controller, I'm putting it in its own .js file and including it based on the controller name from the layout like such:

<%= javascript_include_tag "application开发者_开发技巧", params[:controller] %>

That works just fine, but when I deploy the app to production (I'm using Capistrano and have a pre-compile task set up), the asset pipeline doesn't precompile any of the controller specific JS files. I presume this is because my actual JavaScript file isn't referenced by require directives in application.js.

How do I deal with this without moving my controller specific JS back to application.js, or explicitly referencing it from application.js?

Is there some way to tell the asset pipeline to pre-compile an additional list files? How could I manually pre-compile a specific file on production?

Update

As it turns out, you can specify individual files here in your config/environments/production.rb:

config.assets.precompile += %w( achievements.js )

...or I just went ahead and capriciously added it for every JavaScript file:

config.assets.precompile += %w( *.js )


If you want to precompile the js|css only found in the root of assets/javascripts and assets/stylesheets directories (and not their tree hierarchy), you can put this in environment files :

  Dir.chdir "#{Rails.root}/app/assets/javascripts"
  a = Dir.glob("*.{js,coffee,erb}")
  Dir.chdir "#{Rails.root}/app/assets/stylesheets"
  b = Dir.glob("*.{css,erb}")
  config.assets.precompile +=  a.concat(b)
  Dir.chdir Rails.root


I think you and james_schorr are not really talking about the same thing. You need to add the files other than application.js to config.assets.precompile. His answer was more about the directory structure you could/should adopt, if I'm not mistaken.

If I wanted to have controller specific, I would do:

/assets
    /javascripts
        /users
            login.js
            profile.js
        /blogs
        /posts
        users.js
        blogs.js
        posts.js

And for instance, users.js would be:

*= require_tree ./users

That way, you can stay organized (have a lot of js files per controller), but in prod, they will all be included in one file.

Still need that in your config:

config.assets.precompile += %w( *.js )


This is what I do:

directory structure:

app/assets/javascripts/sessions/multiple.js
app/assets/application-sessions.js

application-sessions.js just has:

 *= require_self
 *= require_tree ./sessions

Then in your view, do

<% if @current_controller == 'whatever' %>
   <%= javascript_include_tag "application-sessions" %>
 <% else %>
   ….
 <% end %>

FYI, @current_controller = controller_name in my application_controller.rb methods, called with a before_filter.


I am having same issue here, it's an headache to me.

Including *.js is a little too much, I don't want every files to be compiled into separated files, some of them suppose to be merged together.

My idea is, store the controller specific files into a sub directory, such as "controllers", and then only include controllers/*.js or css files for precompile.

Not sure if it work or not, I will try it out, anyway, it might be a useful hint.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜