-
Notifications
You must be signed in to change notification settings - Fork 0
rails 3 0 jquery
This is a guide for developers using the example apps from the Rails Apps repository. Others may find it helpful as well.
The example applications don’t make use of jQuery. But many Rails developers prefer it as an alternative to the Prototype Javascript framework so instructions are included here for Rails 3.0 projects.
If you are creating an application template, this step uses the jquery recipe from the rails_apps_composer repository.
Rails 3.1 uses jQuery by default so no additional effort is required.
Here’s how to manually install jQuery for Rails 3.0.
If you used the -J
flag when you ran rails new
the Prototype Javascript files won’t be there. If not, remove the Prototype Javascript files from the public/javascripts directory:
- controls.js
- dragdrop.js
- effects.js
- prototype.js
Download jQuery and rails.js and place them in the public/javascripts directory.
cd public/javascripts curl -o rails.js https://github.com/rails/jquery-ujs/raw/master/src/rails.js curl -o jquery.js http://code.jquery.com/jquery-1.6.min.js
If you used the -J
flag when you ran rails new
the following statement will be uncommented and you don’t need to do anything. Otherwise, uncomment the following statement in the config/application.rb file (or add it if it is missing):
config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
This configures the template helper javascript_include_tag :defaults
to generate SCRIPT tags to load jQuery and the rails.js script.
If you used the -J
flag when you ran rails new
it is best to manually install jQuery as described above.
As an alternative to manual installation of jQuery for Rails 3.0, you can add a gem and run a generator command.
# Gemfile gem 'jquery-rails', '>= 0.2.7'
Run the generator command (add --ui
if you want the optional jQuery UI library):
$ bundle install $ rails generate jquery:install
This will remove the Prototype files from the public/javascripts directory, download jQuery files, and download the rails.js file. Be sure to choose to overwrite the rails.js file (or remove it first).
Note: If you used the -J
flag when you ran rails new
(using the “-J” flag to eliminate the Prototype files) then in the config/application.rb file the statement config.action_view.javascript_expansions[:defaults] = %w()
is not commented out and the jquery-rails gem lib/jquery-rails.rb file cannot override config.action_view.javascript_expansions[:defaults]
to include the jquery files and you’ll get an error “No expansion found for :defaults” when you run your app. See this issue for more information. The solution is to comment out the statement config.action_view.javascript_expansions[:defaults] = %w()
in the config/application.rb file (it takes precedence over the jquery-rails gem lib/jquery-rails.rb file).