【问题标题】:Installing jQuery in Rails 3.2.13在 Rails 3.2.13 中安装 jQuery
【发布时间】:2013-06-27 14:05:31
【问题描述】:

我无法让 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>

这是我的 gem 文件:

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

【问题讨论】:

  • 这种格式的问题不会带你到任何地方。你试过什么?你能发布一些代码吗?您当前的设置是什么。
  • 我在 application.html.erb 文件中包含了这个:。然后我在 application.js 文件中编写了简单的 javascript: $(document).ready(function() { alert( "感谢访问!" ); });
  • 您能发布您的Gemfileapplication.js 吗?我认为 jquery (jquery-rails gem) 自动化包含在 rails 3.2.x 中。
  • 编辑您的问题以添加您的代码示例和“真实”问题。

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


【解决方案1】:

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

<%= 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了解用法

【讨论】:

  • 感谢您的帖子。我仍然收到错误消息。我创建了一个控制器测试,我在该控制器内定义了索引。然后我制作了一个 index.html.erb 文件,其中没有任何内容。然后我尝试加载页面并收到此错误: Uncaught TypeError: Property 'alert' of object [object Object] is not a function application.js:19 (anonymous function) application.js:19 fire jquery-1.9.1.js :1038 self.fireWith jquery-1.9.1.js:1149 jQuery.extend.ready jquery-1.9.1.js:434 完成
  • @MichaelSmith,你能发帖application.js吗?
  • 我还尝试将 jquery-1.10.1.js 的副本添加到 app/assets/javascripts/ 中。这会导致相同的错误。感谢您的帮助!
  • 您没有将 jquery-1.10.1.js 复制到资产中,我已经尝试在我的应用程序上发出警报,它工作正常,您可以在控制台浏览器上看到。
  • 好的,太棒了,它可以在 Safari 中使用。但是,它在 chrome 中出错:未捕获的 TypeError:对象 [object Object] 的属性“警报”不是函数 application.js:19(匿名函数) application.js:19 fire jquery.js:3075 self.fireWith jquery。 js:3187 jQuery.extend.ready jquery.js:434 完成 jquery.js:105
【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多