A Helper gem for both Rails and Sinatra for toggling between a local jquery.js file or the hosted script on Google's CDN based on your application environment, avoiding network dependency during development and bandwidth usage when your application goes live. It also supports the same behavior for a loading the jQuery UI script.
To know more about the benefits of using a CDN for your common Javascript frameworks, check this Chris Coyier post at CSS-Tricks.
If you're using Bundler, just add the jquery_tag
to your Gemfile and run bundle install
. If not, gem install jquery_tag
and require 'jquery_tag'
inside your application code.
To download the latest jQuery script to your application just run jquery_tag
command inside your Terminal and the script will be downloaded to public/javascripts/jquery.js
. You can check the available options running it with the -h
flag.
Inside your views, you can just call the jquery_tag
method.
<%= jquery_tag %>
It accepts a some configuration options by using the following symbols:
:version # Overrides the script version on the CDN URL. Defaults do '1.7.0'
:file # Path for the local script. Defaults do 'jquery.js'
:ui # Loads jQuery UI. Accepts a true value for loading a 'jquery-ui.js' file or a String for the local path.
Any other arguments will be passed along to the javascript_include_tag
helper, if inside a Rails application. The Sinatra Helper currently ignores any extra option.
To use the jquery_tag
on your Sinatra applications, just include the JqueryTag::SinatraHelper
on your application
require 'jquery_tag' # if your not using Bundler.
class Application < Sinatra::Base
helpers do
include JqueryTag::SinatraHelper
end
end
Just call the jquery_tag
helper inside your views or layouts, as in the example above. Your local scripts should be located inside a ./public/javascripts/
folder (or whatever the Sinatra public path is configured on your application).