jested在nested_form gem上发生冲突

时间:2012-07-15 12:26:15

标签: jquery ruby-on-rails-3.2

所以,我认为这可能是我作为JS newb以及最近的Rails升级的问题。我正在尝试使用Ryan Bates的nested_form gem来创建一个复杂的动态嵌套表单。该页面加载正常,看起来很好,但当我点击“添加”和“删除”链接时,没有任何反应。将鼠标悬停在链接上会给我javascript:void(0)。在Chrome中使用JavaScript观察器,我得到了

Uncaught TypeError: Object #<HTMLDocument> has no method 'observe'

一些谷歌搜索告诉我,这意味着我与Prototype存在冲突,但我在我的应用程序中的任何地方都没有使用Prototype(或者除了jQuery之外的任何东西)。

这是我对相关信息的猜测。如果我遗漏了任何东西,请告诉我。

我在Ubuntu 12.04 LTS上使用rails 3.2.4 rc 1和ruby 1.9.3。

我的宝石文件:

source 'http://rubygems.org'
source 'http://gems.github.com'
gem 'rails', '3.2.4.rc1'

gem 'mysql'

gem 'execjs'

gem 'therubyracer'

gem 'nested_has_many_through'
gem 'validates_date_time'
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'
  gem 'therubyracer', :platform => :ruby
  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails', '>= 1.0.12'
gem 'formtastic', :git => 'git://github.com/justinfrench/formtastic.git', :branch => '2.1-stable'
gem "nested_form", :git => 'git://github.com/ryanb/nested_form.git'

来自application.js的我的树

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

资产/ javascripts的内容:

application.js
nested_form.js
rails.js

公共/ javascripts中没有任何内容。

最后,这是控制台在页面加载期间发出的内容(它是Play的编辑页面,其中有很多行为 - 我试图使用nested_form来添加和删除Acts)

[2012-07-15 08:10:28] INFO  WEBrick::HTTPServer#start: pid=23455 port=3000


Started GET "/acts/1/edit" for 127.0.0.1 at 2012-07-15 08:10:32 -0400
Processing by ActsController#edit as HTML
  Parameters: {"id"=>"1"}
  Act Load (3.5ms)  SELECT `acts`.* FROM `acts` WHERE `acts`.`id` = ? ORDER BY act_number LIMIT 1  [["id", "1"]]
  Play Load (0.7ms)  SELECT `plays`.* FROM `plays` WHERE `plays`.`id` = 1 LIMIT 1
  Scene Load (0.9ms)  SELECT `scenes`.* FROM `scenes` WHERE `scenes`.`act_id` = 1 ORDER BY scene_number
  Rendered acts/_scene_fields.erb (2.4ms)
  Rendered acts/_scene_fields.erb (1.0ms)
  Rendered acts/_scene_fields.erb (1.0ms)
  Rendered acts/_scene_fields.erb (1.0ms)
  Rendered acts/_scene_fields.erb (1.0ms)
  Rendered acts/_scene_fields.erb (1.0ms)
  Rendered acts/_scene_fields.erb (1.0ms)
  Rendered acts/_scene_fields.erb (1.0ms)
  Rendered acts/_scene_fields.erb (1.0ms)
  Rendered acts/_scene_fields.erb (1.0ms)
  Rendered acts/_scene_fields.erb (1.3ms)
  Rendered acts/edit.html.erb within layouts/application (317.0ms)
Completed 200 OK in 558ms (Views: 407.1ms | ActiveRecord: 12.7ms)
[2012-07-15 08:10:33] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2012-07-15 08:10:33 -0400
Served asset /jquery.js - 304 Not Modified (30ms)
[2012-07-15 08:10:33] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/formtastic_changes.css?body=1" for 127.0.0.1 at 2012-07-15 08:10:33 -0400
Served asset /formtastic_changes.css - 304 Not Modified (3ms)
[2012-07-15 08:10:33] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/formtastic.css?body=1" for 127.0.0.1 at 2012-07-15 08:10:33 -0400
Served asset /formtastic.css - 304 Not Modified (5ms)
[2012-07-15 08:10:33] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/nested_form.js?body=1" for 127.0.0.1 at 2012-07-15 08:10:33 -0400
Served asset /nested_form.js - 304 Not Modified (3ms)
[2012-07-15 08:10:33] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2012-07-15 08:10:33 -0400
Served asset /application.css - 304 Not Modified (9ms)
[2012-07-15 08:10:33] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true


Started GET "/assets/scaffold.css?body=1" for 127.0.0.1 at 2012-07-15 08:10:33 -0400
Served asset /scaffold.css - 304 Not Modified (3ms)
[2012-07-15 08:10:33] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true

我怀疑有些东西我不小心在某个地方打电话,或者其他什么......但我不知道。

2 个答案:

答案 0 :(得分:1)

SUCCESS!

我明白了......但我仍然不太明白为什么这些答案是正确的答案。谢谢大家的帮助和建议。事实证明,我必须做三件事。

1)@lest是正确的,我需要从我的assets / javascripts文件夹中取出nested_form.js。 2)我不得不从调用javascript_include_tag:defaults切换到javascript_include_tag'application'。 3)我不得不在application.js中重新排序我的树,如下所示(我之前已经交换了jquery和jquery_ujs,根据我在stackoverflow上读到的其他内容的建议):

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

答案 1 :(得分:0)

Prototype附带Rails,即使您没有直接使用它。我之前遇到过一些冲突。您可以尝试another Stack Overflow post上提到的建议,也就是说:

  

使用jQuery.noConflict();在此之后,所有对jquery的调用都应该是   仅使用jQuery()而不是$()

完成

(顺便说一句,我不清楚你在编写的代码或第三方代码中是否发生了这个错误。即使是在第三方代码中,你也可以尝试修改它以使用jQuery()只有错误的部分,看看是否能让你超越冲突。)_