In our application we have a gem that supplies common code for five web sites running Rails applications. In the gem in the file lib/theme/controller_module.rb is the following code:
def application_javascripts(*items)
@application_javascripts ||= []
@application_javascripts += items
end
In the gem in the file helpers/theme_helper.rb is the following code:
def application_js_include_tag
if @application_javascripts
javascript_include_tag( *@application_javascripts)
end
end
The theme gem is in each application’s Gemfile. In controller code and in helpers the application_javascripts executes to track which page-specific JavaScript is used on the page and the application_js_include_tag is placed at the bottom of the view to make only those few files available.
Unfortunately, it appears that @application_javascripts value is not updated in the view once the view has started to render. Additions to @application_javascripts that occur in a helper are not available to the view when application_js_include_tag is executed at the bottom of the page. Do I need a separate helper like application_javascripts with a variable defined there, and use both in application_js_include_tag? Or some other solution?
From its behavior, it appears that a snapshot of
@application_javascriptsis taken at the time rendering starts. Execution ofcontroller.application_javascriptsduring the rendering does not change the value of@application_javascriptsavailable in the view. Defining inhelpers/theme_helper.rb:also does not change the value. What does work, is to reproduce the code so that
@application_javascriptsis changed by the code resident in the helper file. Defining inhelpers/theme_helper.rba duplicate of the controller code:works fine.