In Ruby on Rails, how to ensure files include the same javascript file only once?

In Ruby on Rails, if a partial (such as _msgbox.html.erb) need to use Javascript by

javascript_include_tag :defaults

but then, the page layout or other view may also have that same line, so the same javascript files will be included multiple time?

Is there a way to tell Rails just to include it at most once? kind of like the PHP ways of require_once or include_once

you need to include the javascript in the layout so that you do not need to include it in other files.

If it’s already included in the layout, you won’t need it in the partial. If you are wanting to customize the js loaded for each page, so you can better control load times, I would look into the content_for method.

A great explanation can be found here http://guides.rubyonrails.org/layouts_and_rendering.html in sections 3.2 and 3.3

Hope this helps.