Sometimes, usually in development, I find it useful to have my Javascripts printed inline rather than using the <script src="/path/to"> tags that are generated by javascript_include_tag.
With javascript_inline_tag you can simply declare your Javascripts just like you would with javascript_include_tag and it will spit out the script into your view/layout.
Put it where you like, but I chose to keep with my application_helper.rb.
module ApplicationHelper
# ..... your app helpers, per usual
end
module ActionView::Helpers::AssetTagHelper
def javascript_inline_tag(*sources)
if sources.include?(:defaults)
sources = sources[0..(sources.index(:defaults))] +
@@javascript_default_sources.dup +
sources[(sources.index(:defaults) + 1)..sources.length]
sources.delete(:defaults)
sources << "application" if defined?(RAILS_ROOT) && File.exists?("#{RAILS_ROOT}/public/javascripts/application.js")
end
sources.collect do |source|
source = javascript_path(source).sub(/\?\d+/,'')
contents = ''
File.open("#{RAILS_ROOT}/public#{source}").each do |line|
contents << line
end
javascript_tag(contents)
end.join("\n")
end
end
Sample Usage
<%= javascript_inline_tag :defaults,'niftycube' %>
Notes
- You can not pass an
optionshash. - For production, I’ll just say, consider caching (browser&server) and page size.
- Nifty Corners Cube is an awesome script that I recently discovered.
- You may find
render :file => ''to work for you, I just thought it wasn’t flexible enough for this situation.





