在Rails中安装jQuery 3.2.13

时间:2013-06-27 14:05:31

标签: jquery ruby-on-rails ruby user-interface install

我无法让jQuery在Ruby on Rails 3.2.13上工作。我看了教程,搜索了youtube等等,我仍然无法让它工作。

更具体地说,我在使用jQuery ui时遇到了麻烦。我无法让手风琴小部件工作。我甚至无法获得简单的jQuery(比如显示一个hello警告消息)来处理ROR。

非常感谢您的帮助。谢谢!

这是我的application.html.erb文件:

 <!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>

这是我的宝石文件:

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'

她是我的application.js文件:

// 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!');
});

2 个答案:

答案 0 :(得分:4)

Jquery在Rails 3.2.13中自动化,请参阅你的Gemfile,如果你的Gemfile上有gem 'jquery-rails',你应该使用'application',没有'jquery-1.10.1.js',jquery-rails最新版本使用jQuery 1.10.1 see here

<%= javascript_include_tag :application %>

这将加载app/assets/javascript/application.js文件以加载app/assets/javascript文件夹中的所有其他javascript文件,包括jQuery:

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

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

如果您想使用JQuery UI,可以使用jquery-ui-rails gem https://github.com/joliss/jquery-ui-rails

在你的Gemfile中,添加:

gem 'jquery-ui-rails'

并运行bundle install

阅读here了解用途

答案 1 :(得分:2)

检查浏览器的控制台是否有任何加载错误。您似乎没有部署javascript,因此未加载。

使用

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

或拥有您在任何

中提供的文件的副本
/lib/assets/javascripts
/vendor/assets/javascripts
/app/assets/javascripts