Skip to content

Instantly share code, notes, and snippets.

@humanzz
Created July 8, 2010 16:20
Show Gist options
  • Save humanzz/468245 to your computer and use it in GitHub Desktop.
Save humanzz/468245 to your computer and use it in GitHub Desktop.
A rails helper to help you organize javascripts in your views
module JavascriptsHelper
# This helper yeilds contents to 2 places that should be available
# in the application layout. One for including javascript files,
# the other is for including initialization codes. The layout should have
# something as follows:
# <%= yield :javascripts %>
# <script type="text/javascript">
# $(function({
# <%= yield :javascripts_ready %>
# }));
# </script>
# Example usage in a certain page:
# <% javascripts 'file1', 'file2' do %>
# alert('welcome here!');
# <% end %>
def javascripts(*args, &block)
content_for(:javascripts) {javascript_include_tag(*args)} if args.length > 0
content_for(:javascripts_ready, &block) if block_given?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment