質問

What is the best way to split Javascript assets into scripts that need to load in the header and scripts that need to be in the body?

For instance, right now I have,

<%= javascript_include_tag "application", "data-turbolinks-track" => true %>

At the end of my body tag in my main application layout. But I want to include something like,

<%= javascript_include_tag "application_head", "data-turbolinks-track" => true %>

Inside my header tag.

How do I split my application.js into two separate files to accomplish this? Keep in mind that I need to be able to use direction likes require_tree in both files so I can't just include each script individually.

役に立ちましたか?

解決

$ mkdir app/assets/javascripts/application
$ mkdir app/assets/javascripts/application_head

copy stuff you want in the head into the application_head directory and the rest into application

app/assets/javascripts/application_head.js:

//= require_self
//= require_tree ./application_head

app/assets/javascripts/application.js:

//= require_self
//= require_tree ./application

in config/application.rb, add

config.assets.precompile << 'application_head.js'
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top