Hi all,
Sorry if this has already been discussed, but I haven’t found any post
matching what I’m looking for. (If there is, please point me to it!)
I’d like to include a javascript in the header section, but just for one
view. I went through the guides and I understand you can do something
like
this inside a particular view:
<% content_for :head do %>
<% end %>
However, if I add myJavascript.js in app/assets/javascripts, it gets
loaded
for every single page. Years ago, when I did RoR development (before
assets), I used to put javascripts in the public/javascripts. Is that
still where I should put theses one-off javascript files? What is the
best
practice now?
Thanks for any pointers!
Kumi
unknown wrote in post #1154022:
Hi all,
Sorry if this has already been discussed, but I haven’t found any post
matching what I’m looking for. (If there is, please point me to it!)
I’d like to include a javascript in the header section, but just for one
view. I went through the guides and I understand you can do something
like
this inside a particular view:
<% content_for :head do %>
<% end %>
However, if I add myJavascript.js in app/assets/javascripts, it gets
loaded
for every single page.
By default a new Rails application will load all javascript/coffeescript
files under the app/assets/javascripts directory and all subdirectories.
Look for the following line in your application.js manifest file:
//= require_tree .
That does what it sounds like it would. It loads all javascripts in the
entire subtree. This might be fine for very simple application, or for
getting started, but probably not what you want in a more complex
application.
This is explained in further detail here:
Thanks, Ruby-Forum.com. I did have //= require_tree . in the manifest
and
didn’t think too much about it, so thanks for pointing that out!
I had gone through that documentation, but I just wasn’t finding the
exact
info I was looking for. It really isn’t a controller-specific
javascript,
it’s a view-specific one, and I didn’t want it in the manifest
(application.js). But anyway, I did finally figure out one way to
accomplish this after going through the doc again and some good old
search.
Here’s what I ended up doing in case someone else was wondering the
same.
If you have a better solution, please do add a reply!
Inside myView.html.erb - the only view that needs this vendor js:
(javascript file is at
APP_ROOT/vendor/assets/javascripts/subfolder/vendorJavascriptName.js)
<% content_for :head do %>
<%= javascript_include_tag “subfolder/vendorJavascriptName”,
‘data-turbolinks-track’ => true %>
<% end %>
Inside the layout (application.html.erb), add <%= yield :head %>
Blog
<%= stylesheet_link_tag 'application', media: 'all',
'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' =>
true
%>
*<%= yield :head %>*
<%= csrf_meta_tags %>
<%= yield %>
Inside config/environments/development.rb
config.assets.precompile += %w( subfolder/vendorJavascriptName.js )
After these three changes are made, restart server and it works as
expected
with the one view getting this extra vendor javascript.
Note that I didn’t have a config/initializers/assets.rb like the
documentation says (precompiling section), so I edited development.rb
instead. Can I just simply add a assets.rb file so all environments gets
this same line?
Thanks,
Kumi