Ajax on Rails
by Scott Raymond
The unconfirmed error reports are from readers. They have not yet been
approved or disproved by the author or editor and represent solely the
opinion of the reader.
Here's a key to the markup:
[page-number]: serious technical mistake
{page-number}: minor technical mistake
: important language/formatting problem
(page-number): language change or minor formatting problem
?page-number?: reader question or request for clarification
This page was updated May 14, 2008.
UNCONFIRMED errors and comments from readers:
[1] -;
Where is the source code to accompany the book?
[4] Code for 'whet.rjs';
The example produces error message popups, beginning with
"RJS error: TypeError: $$("form").first() has no properties".
(10) 2nd sentence;
Perhaps "List" should be "Lisp" here:
"roots in List, Perl, and Smalltalk."
(12) 1st paragraph in the section "Starting Ruby";
A more up to date set of instructions to install Ruby on MacOSX 10.4 (tiger)
can be found at http://hivelogic.com/articles/ruby-rails-mongrel-mysql-macosx
(17) Fig 1-6;
In the diagram, it states "Articles ia an ActiveRecord obeject..".
The object should be singular "Article"
(20) Paragraph after illustration;
Files are no longer generated with .rhtml. They are now .html.erb This is due to a
change in AJAX itself since publication.
[23] 2nd code, 3rd line from the bottom;
request.send() should be request.send(null)
(30) 1st Paragraph after heading "Back to Ajax";
There is an example at this point:
<%= link_to_remote "Alert with Javascript Helper", :url =>
"/chapter2/myresponse", :success => "alert(request.responseText)" %>
which gives a strange response. Instead of getting the alert with the server
response, as expected, I get an alert with the full page of the response,
including all the html from ... . It's almost as if it was
expected to output a full page...?
I expected only the simple server response in an alert box.
If this is what is expected, then a screen shot would be very helpful.
{32} 2nd paragraph... script/generate code;
The command:
script/generate controller chapter3 get_time repeat reverse
does NOT generate the index action as stated in paragraph 3.
[32] First command;
script/generate controller chapter3 get_time repeat reverse
should be
script/generate controller chapter3 index get_time repeat reverse
{33} na;
Let's get the example code such as that on p.33 on the website for downloading!!
{34} get_time method;
I think the line of code "render :text => Time.now"
should be "render :text => Time.now.asctime"
or the update to current time will fail on the top of pg 36.
(34) Line 19;
'Time.now' should be 'Time.now.to_s'
(former doesn't work with 1.8.4 on Ubuntu, anyway)
[34] Chapter3Controller code block;
Time.now produces a NoMethodError for the method 'size' when called with 'render :text
=> Time.now' . This causes a fatal for the page that follows;
....
NoMethodError in Chapter3Controller#get_time
undefined method `size' for Fri Jan 26 09:54:27 -0800 2007:Time
....
This fatal happens because the 'render' helper is trying to operate on the Time object's
size method which it doesn't have. Instead should be working on a String object (or
something similar) which does have 'size'.
It can simply be fixed by using one of Time object's methods that returns a String
object like so;
...
def get_time
sleep 1.second
render :text => Time.now.to_s
end
...
-------------
Environment
Mac OS X 10.4.8
ruby 1.8.5 (2006-12-04 patchlevel 2) [i686-darwin8.8.1] (complied from source)
Rails 1.2.0 (installed from gem)
*** LOCAL GEMS ***
actionmailer (1.3.0)
Service layer for easy email delivery and testing.
actionpack (1.13.0)
Web-flow and rendering framework putting the VC in MVC.
actionwebservice (1.2.0)
Web service support for Action Pack.
activerecord (1.15.0)
Implements the ActiveRecord pattern for ORM.
activesupport (1.4.0)
Support and utility classes used by the Rails framework.
rails (1.2.0)
Web-application framework with template engine, control-flow layer,
and ORM.
rake (0.7.1)
Ruby based make-like utility.
sources (0.0.1)
This package provides download sources for remote gem installation
{34} reverse method;
I think the code line
@reversed_text = params[:text_to_reverse].reverse
should be
render :text => params[:text_to_reverse].reverse
or the first use of reverse (pg 39) will not work correctly. After that it needs to be
changed back to the original version prior to its use on page 41.
{34} towards middle, in code snippet def get_time;
render :text => Time.now
should be
render :text => Time.now.to_s
It seems if you don't do this Rails will barf because it will try to determine the
content length by calling length on the time object.
{35} 3rd paragraph: "If you view the source...";
Uh... just how do you "view the source..."? I added the preceding "link_to" example:
<%= link_to "Delete", "/people/1", :method => :delete %>
reloaded the page, and then did a "view page source" in my browser, and the only thing
that was displayed was:
Delete
[38-39] starting from bottom of page 38 (last paragraph...);
There is a whole series of examples that don't seem to work properly or are not
explained properly... they're certainly confusing when you try to get them to run. They
all use actions that were created as part of Chapter 3 ('repeat', 'reverse') so the
assumption I made is that these were working snippets of code, just like was the case in
Chapter 2.
Here are the problems I encountered:
1. Page 38, last para: When I submit the fake form, I get an alert with the text:
{"action"="repeat","foo"=>"bar","controller"=>"chapter3"}
2. Page 39, 2nd para, 1st example: When I click the link "Reverse field", the input
field is filled with HTML (from the repeat.rhtml action, I believe): ""bar","controller"=>"chapter3"}
4. Page 39, 5th para, helper output code: I get something different:
Link with params
5. Page 39, 6th para, last example: when I click the link "Link with dynamic params", I
get the same alert box as in #1 and #3 above:
{"action"="repeat","foo"=>"bar","controller"=>"chapter3"}
(39) 1st bit of code;
Sorted by changing reverse def in Chapter3Controller to:
def reverse
#@reversed_text = params[:text_to_reverse].reverse
render :text => params[:text_to_reverse].reverse
end
{40} 3rd paragraph, example code;
The book says that the example:
<%= form_tag ( :action => 'reverse') %>
<%= end_form_tag %>
generates this...
Actually, it generates this...
Note that the closing has been embedded into the opening
") at the end of the first two lines of the code
example, but only a single paragraph start tag (""). Either one of the closing tags
needs to go or an appropriate start tag needs to be added.
{54} Visual Effect Helper def get_time;
Unnecessary here as the following chunk of code uses the chapter3 controller get_time
{56} First single-line code example;
"
drag
" => "drag
".
{57} changeDiv code;
Paragraph warns of necessity for two sets of quote marks. Code does not show it:
:constraint => 'vertical'
should be:
:constraint => "'vertical'"
[58] 2nd para under 'Droppables';
In Rails 2.0.2, at least. One cannot have just
<%= drop_receiving_element :drophelperdiv, :hoverclass => 'hover' %>
It generates a run time error. This is fixed by adding parameters after the hover:
<%= drop_receiving_element(:drophelperdiv, :hoverclass => 'hover',
:update => "status", :url => {:action => "receive_drop" } ) %>
I didn't analyze it further than that...
{59} Droppables with Ajax;
In the following code, you may get an error in IE 6 (maybe others) stating "Object
doesn't support this property or method. If you change each instance of "drop" to
something else, the error goes away.
Existing code:
...
div id="drop" class="pink box">drop
<%= drop_receiving_element :drop, :hoverclass => "hover",
:update => "status", :url => { :action => "receive_drop" } %>
...
Fixed code:
...
div id="dropWithAjax" class="pink box">dropWithAjax
<%= drop_receiving_element :dropWithAjax, :hoverclass => "hover",
:update => "status", :url => { :action => "receive_drop" } %>
...
[63] Description of onUpdate;
Each item must have ID attribute whose value is in the format xxx_n.
{70} 3rd code example;
application.css defines box text-transform property as 'uppercase'; comment out that
property to exercise the upcase custom method
[71] Entire section;
I'm not sure if this is a result of version changes, or something else.
Code such as:
page[:my_div].replace_html "New Text"
Didn't work for me.
I had to do:
page.replace_html 'my_div' "New Text"
Is there a reason why the former did not work?
(72) First paragraph of the "Collection Proxies" section;
"...method ($$()) in Prototype. The $() method is used..." => "...method ($$()) in
Prototype. The $$() method is used..."
(73) First paragraph of the "each" section;
"...items that descend from the element identified as items..." => "...items that
descend from the element identified as todo...", or "...items that descend from the
element whose id is todo..."
{73} Code example for the "pluck" section;
The code example uses the "page <<" method, which is cool, but hasn't been explained
before or in this section.
(74) First paragraph of the "select/find_all" section;
"...finds all the members of the collection for which the block is true and store them
in a JavaScript object." => "...finds all the members of the collection for which the
block is true and stores them in a JavaScript array."
(75) Last line under 'all and any';
variable result will be true --> variable any will be true
{93} in code snippet, near bottom of page;
the part like this:
String.fromCharCode(event.keyCode)
should have been:
String.fromCharCode(event.charCode)
{115} under Routing Navigator;
Apparently the latest version of the routing navigator only works with edge rails
and you also need to add the following to your application controller or
individual controller to enable the routing navigator as it is off by default:
routing_navigator :on
{164} 3rd pagagraph, campaign_url or campaign_urls;
In the first line of the form, it uses ":url => campaigns_url", but in the
previous code it is referred to as "campaign_url", singular. It appears the
line in the form is not correct.
(241) 3rd paragraph, 3rd Sentence "Once the files are in the correct place...";
If you do not >rake freeze_edge will receive an error when you run
>rake db:schema:load.
Here's the error message:
"undefined method `first for :quizzes:Symbol"
------------
This Review Quiz sample currently generates an error and does not run the
schema file to create the tables unless you >rake freeze_edge in the rails
directory.
I suggest you change the the paragraph to include an instruction to
>rake freeze_edge.
Here is suggested change: change sentence to "Once the files are in the correct
place, change to the root directory of the quizzes application and run the
command >rake freeze_edge to update the copy of Review Quiz you downloaded. Also,
you'll need to configure a database..."
{260} 1st paragraph;
there is: username root and no password
should be: username admin and password admin
{262} bottom page;
there is: require'rmagick'
should be: require 'RMagick'