So, I think this is maybe a problem with me being a JS newb and also a recent Rails upgrade. I’m trying to use Ryan Bates’ nested_form gem to create a complex dynamic nested form. The page loads ok and looks fine, but when I click the “add” and “remove” links, nothing happens. Hovering over the links gives me javascript:void(0). Using the JavaScript observer in Chrome, I get
Uncaught TypeError: Object #<HTMLDocument> has no method 'observe'
Some googling tells me that this means I’m having a conflict with Prototype, but I am not using Prototype (or anything but jQuery) anywhere in my application.
Here’s my guess at the relevant info. Please tell me if I’ve left anything out.
I’m using rails 3.2.4 rc 1 and ruby 1.9.3, on Ubuntu 12.04 LTS.
My gemfile:
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'
My tree from application.js
//= require jquery_ujs
//= require jquery
//= require jquery_nested_form
//= require_tree .
Contents of assets/javascripts:
application.js
nested_form.js
rails.js
There isn’t anything in public/javascripts.
Lastly, here’s what the console puts out during page load (it’s for the edit page of a Play, which has_many Acts–I’m trying to use nested_form to add and remove 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
I suspect there’s something I’m accidentally calling somewhere, or something…but I have no idea.
SUCCESS!
I figured it out…but I still don’t quite understand why these answers are the right answers. Thank you all for your help and suggestions. It turned out that there were three things I had to do.
1) @lest was correct that I needed to take nested_form.js out of my assets/javascripts folder.
2) I had to switch from calling javascript_include_tag :defaults to javascript_include_tag ‘application’.
3) I had to reorder my tree in application.js as follows (I previously had jquery and jquery_ujs swapped, at the recommendation of something I read elsewhere on stackoverflow):