2013-06-27 115 views
2

我无法让jQuery在Ruby on Rails 3.2.13上工作。我看了教程,搜索了YouTube等,但仍然无法实现。在Rails 3.2.13中安装jQuery

更具体地说,我在使用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!'); 
}); 
+1

此格式的问题,不会让你的任何地方。你有什么尝试?你可以发布一些代码吗?你目前的设置是什么。 – Arjan

+0

我在application.html.erb文件中包含了这个:<%= javascript_include_tag'jquery-1.10.1.js','application'%>。然后我在我的application.js文件中写入了简单的javascript:$(document).ready(function(){alert(“Thanks for visiting!”); }); –

+0

你可以发布你的'Gemfile'和'application.js'吗?我认为jquery(jquery-rails gem)自动化包含rails 3.2.x. –

回答

4

的jQuery自动化所服务的文件的副本中的Rails 3.2.13,看到你的Gemfile,如果你有gem 'jquery-rails'在你的Gemfile上,你应该使用'application',没有'jquery-1.10.1.js',jquery-rails最新版本使用jQuery 1.10.1see 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宝石https://github.com/joliss/jquery-ui-rails

在你的Gemfile,加:

gem 'jquery-ui-rails'

和运行bundle install

阅读here为使用

+0

感谢您的留言。我仍然收到错误。我创建了一个控制器测试,我在该控制器中定义了索引。然后我做了一个没有任何内容的index.html.erb文件。然后我尝试加载页面,并得到这个错误:未捕获TypeError:对象[对象对象]属性'警报'不是一个函数application.js:19 (匿名函数)application.js:19 火灾jquery-1.9.1 .js:1038 self.fireWith jquery-1.9.1.js:1149 jQuery.extend.ready jquery-1.9.1.js:434 completed –

+0

你可以发布'application.js'吗?编辑和发布您的问题.. –

+0

@MichaelSmith再次,你可以发布'application.js'? –

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 
+0

包括jquery解决了长时间的问题。 – thesummersign