views:

16

answers:

2

Is it possible in rails using a filter that appears, after the view (after_filter), to collect the data that has appeared inside of the javascript_include_tag and stylesheet_link_tag methods?

for example:

---rails view starts
javascript_include_tag 'file.js'

---rails view is complete and after_filter runs
@js_files = get_included_javascript_files_array

Any ideas?

A: 

If would suggest that if you need to figure out which js files are being included then you are already telling rails which files to include, so just create something in your ApplicationController:

before_filter :setup_js_includes

def setup_js_includes
  @js_includes = [ 'file.js' ]
end

Then in your view:

<%= javascript_include_tag @js_includes %>

I'm assuming that javascript_include_tag can take an array, if it can't then you need to iterate using @js_includes.each in the view.

Then you can do whatever you like in your :after_filer because the @js_includes variable is already available.

John Drummond
A: 

Yes that is what I have done before, however, some of the pages that I have include the javascript or css file within the view.

----- register.html.erb -----
<% content_for(:javascripts) do %>
<%= javascript_include_tag 'register' %>
<% end %>
----- rest of the register page

----- application.html.erb -----
.....
<%= yield(:javascripts) %>
</body>
</html>
----- end of application.html.erb file -----

So what's happening is that when the page gets to the end, it just loads the javascript files that have been populated within the view and any helpers. I would like to fetch these added JS files.

matsko