2

我无法让 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!');
});
4

2 回答 2

4

在 Rails 3.2.13 中实现 Jquery 自动化,请参阅您的 Gemfile,如果您gem 'jquery-rails'的 Gemfile 中有,您应该使用'application',不使用'jquery-1.10.1.js'jquery-rails 最新版本,jQuery 1.10.1 请参阅此处

<%= javascript_include_tag :application %>

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

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

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

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

在您的 Gemfile 中,添加:

gem 'jquery-ui-rails'

并运行bundle install

在这里阅读使用

于 2013-06-27T14:31:17.007 回答
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
于 2013-06-27T14:27:54.587 回答