1

所以,我认为这可能是我作为 JS 新手以及最近的 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

public/javascripts 中没有任何内容。

最后,这是控制台在页面加载期间输出的内容(它用于 Play 的编辑页面,其中 has_many Acts——我正在尝试使用 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

我怀疑有什么我不小心打电话给某个地方的东西,或者什么……但我不知道。

4

2 回答 2

1

成功!

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

1)@lest 是正确的,我需要将nested_form.js 从我的 assets/javascripts 文件夹中取出。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 .
于 2012-07-17T02:21:04.330 回答
0

Prototype ships with Rails, even if you're not using it directly. I've ran into some conflicts before like this. You may try the suggestion mentioned on another Stack Overflow post, which is to say:

use jQuery.noConflict(); and after this all calls to jquery should be done only using the jQuery() instead of $()

(By the way, it's not clear to me if this error is happening in code you've written or in 3rd party code. Even if it's in 3rd party code, you could try modifying it to use the jQuery() in the part that errors only and see if that gets you past the conflict.)_

于 2012-07-15T12:52:04.253 回答