Michael Smith
Michael Smith

Reputation: 3447

Installing jQuery in Rails 3.2.13

I am having trouble getting jQuery to work on Ruby on Rails 3.2.13. I have looked at tutorials, searched youtube, etc. and I still can't get it to work.

More specifically, I am having trouble using jQuery ui. I can't get the accordion widget to work. I can't even get simple jQuery (like displaying a hello alert message) to work on ROR.

Your help is greatly appreciated. Thanks!

Here is my application.html.erb file:

 <!DOCTYPE html>
    <html>
    <head>
      <title>TestProj</title>
      <%= stylesheet_link_tag    "application", :media => "all" %>
      <%= javascript_include_tag "application" %>
      <%= csrf_meta_tags %>
    </head>
    <body>

    <%= yield %>

    </body>
    </html>

Here is my gem file:

source 'https://rubygems.org'

gem 'rails', '3.2.13'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'

Hers is my application.js file:

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree .


$(document).ready(function() {
 alert('Thanks for visiting!');
});

Upvotes: 2

Views: 5634

Answers (2)

rails_id
rails_id

Reputation: 8220

Jquery automate in Rails 3.2.13, see on your Gemfile, if you have gem 'jquery-rails' on your Gemfile, you should be using 'application', without 'jquery-1.10.1.js', jquery-rails latest version using jQuery 1.10.1 see here

<%= javascript_include_tag :application %>

This will load the app/assets/javascript/application.js file to load all your other javascript files on app/assets/javascript folder, including jQuery:

//= require jquery
//= require jquery_ujs
//= require_tree .

$(document).ready(function() {
 alert('Thanks for visiting!');
});

If you want use JQuery UI, you could use jquery-ui-rails gem https://github.com/joliss/jquery-ui-rails

In your Gemfile, add:

gem 'jquery-ui-rails'

and run bundle install

read here for usage

Upvotes: 4

rewritten
rewritten

Reputation: 16435

Check your browser's console for any load error. It seems that you are not deploying the javascript, so it's not loaded.

Either use

 <%= javascript_include_tag "//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" %>

or have a copy of the file you are serving in any of

/lib/assets/javascripts
/vendor/assets/javascripts
/app/assets/javascripts

Upvotes: 2

Related Questions