Problem with RJS and/or Prototype.js =>ResponseText is blank

Hi, I’ve got a real head-scratcher. I’ve got a standard Ajax form using
form_remote_tag, which calls a controller action which adds a comment
and then sends back some JS via RJS which updates a few items on the
page.

It did work fine but (and I’ve no idea what has happened) now it
doesn’t. It calls the controller action but gets an empty response back
and then redirects (it seems) to the controller directly, thus rendering
the javascript in plain text.

If I get rid of the RJS and render a partial and use :update =>
“comments” in my form_remote_tag then it does the same thing but renders
the html partial.

You can check it out by going to the site itself at

http://indecipherable.co.uk/home/index/welcome-to-indecipherable/

Click add comment to show the form and off you go.

The server is mongrel (on my dev box as well, though it does the same
with webrick).

I’ve looked at Firebug and the request goes off ok but no responseText
comes back.

The RJS code looks like this

page.visual_effect “BlindUp”, “add-comment”
page.insert_html :top, “comments”, :partial =>
“/common/comments/comment”, :object => @comment
page.show “show-comment”
page.replace_html “comment-count”,
pluralize(@comment.entry.comments.count, “Comment”)
page[:latest].visual_effect :highlight, :startcolor => “#ffffff” #,
:endcolor => “#b1d0e9

The controller action looks like this

def comment
@comment = Comment.new(:text => params[:text])
@comment.user = User.find(:first)
@comment.entry = Entry.find_by_identifier(params[:entry])
if (@comment.save)
@latest_comment = @comment
# render :partial => “/common/comments/comment”, :object => @comment

This fails too

else
render :action => “show”
end
end

The form looks like this

<% form_remote_tag(:url => { :action => :comment },
:html => {:class => “margin-form” }) do %>

Your Comment:
<%= text_area_tag(:text, “”, :rows => 4, :cols => 50) %>


<%= submit_tag(“add your comment »”, :class => “submit”,
:disable_with => “Sending comment…”) %>

<% end %>

I’ve searched high and low and found a few similar stories but every
time the reason for the odd behaviour hasn’t been related to my code,
which seems to be pretty boilerplate to me.

Thanks for any help.

Here is the header (after the page has refreshed to …/comment) -
nothing unusual (to my eyes).

Response Headers
Cache-Control no-cache
Connection Keep-Alive
Date Tue, 15 May 2007 08:31:50 GMT
Content-Type text/javascript; charset=utf-8
Server WEBrick/1.3.1 (Ruby/1.8.6/2007-03-13)
Content-Length 1213
Set-Cookie
_indecipherable_session_id=e6e8e71a0fa0bbef590ef22814a64126; path=/
Request Headers
Host localhost:3000
User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB;
rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3
Accept
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,/;q=0.5
Accept-Language en-gb,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
Connection keep-alive
Referer http://localhost:3000/home/index/welcome-to-indecipherable/
Cookie style=null;
_indecipherable_session_id=e6e8e71a0fa0bbef590ef22814a64126


The only before_filter that runs populates a couple of globals with some
navigation data from the db. If I stop it from running for the comment
action, it makes no difference.


Here’s the (only) log entry from the action

Processing ContentController#comment (for 127.0.0.1 at 2007-05-15
09:37:12) [POST]
Session ID: e6e8e71a0fa0bbef590ef22814a64126
Parameters: {“text”=>"…", “action”=>“comment”,
“controller”=>“content”, “entry”=>“welcome-to-indecipherable”,
“page”=>“index”, “section”=>“home”}
e[4;36;1mComment Columns (0.000000)e[0m e[0;1mSHOW FIELDS FROM
commentse[0m
e[4;35;1mUser Load (0.000000)e[0m e[0mSELECT * FROM users LIMIT
1e[0m
e[4;36;1mUser Columns (0.016000)e[0m e[0;1mSHOW FIELDS FROM
userse[0m
has_many_polymorphs: INIT
e[4;35;1mEntry Columns (0.016000)e[0m e[0mSHOW FIELDS FROM
entriese[0m
e[4;36;1mTagging Columns (0.016000)e[0m e[0;1mSHOW FIELDS FROM
taggingse[0m
has_many_polymorphs: injecting dependencies
e[4;35;1mEntry Load (0.015000)e[0m e[0mSELECT * FROM entries WHERE
(entries.identifier = ‘welcome-to-indecipherable’) LIMIT 1e[0m
e[4;36;1mSQL (0.000000)e[0m e[0;1mBEGINe[0m
e[4;35;1mSQL (0.000000)e[0m e[0mINSERT INTO comments (entry_id,
text, user_id, created_at) VALUES(2, ‘…’, 1, ‘2007-05-15
09:37:13’)e[0m
e[4;36;1mSQL (0.016000)e[0m e[0;1mCOMMITe[0m
Rendering content/comment
Rendered /common/comments/_comment (0.00000)
e[4;35;1mSQL (0.000000)e[0m e[0mSELECT count(*) AS count_all FROM
comments WHERE (comments.entry_id = 2) e[0m
Completed in 0.74900 (1 reqs/sec) | Rendering: 0.01600 (2%) | DB:
0.07900 (10%) | 200 OK
[http://localhost/home/index/welcome-to-indecipherable/comment]

So, it’s only running the action once, rather than once for the AJAX
call and then again for the regular browser refresh.

It looks like the AJAX call is getting no reply or not getting to it’s
destination and then it falls through to POST the form in the normal
fashion.

Well when I post a comment, Firebug shows me an error, but the page is
redirected too fast for me to see what the error actually is.

Manually running the RJS on the comments page seems to work quite well.

You’ll need to find a way to keep the page from redirecting into the RJS
to
find out what the problem is.

Jason

Ok. I’ve discovered something I was wrong about above.

It does make 2 calls to the comment action - one for AJAX and one for
the POST.

I changed the controller code to add server-side degradability, which
has the effect of making the page work properly (I check request.xhr?).

Now, the log shows 2 entries and 2 entries are posted.

a bad job

Jason R. wrote:

Well when I post a comment, Firebug shows me an error, but the page is
redirected too fast for me to see what the error actually is.

Manually running the RJS on the comments page seems to work quite well.

You’ll need to find a way to keep the page from redirecting into the RJS
to
find out what the problem is.

Jason

Thanks. Yep, the script itself is fine. I’ve been living in Firebug,
stepping through prototype.js for the last day and I can’t for the life
of me work it out.

The call goes through to the controller but no response comes back. I
don’t see an error in Firebug and it doesn’t break if I set “break on
all errors”.

The controller action obviously works because it does send the JS back
to the browser. The JS works, as we’ve seen.

The controller action doesn’t even fire for the AJAX call (checking the
log files).

I can’t see where it calls the controller action with a “manual” POST -
is that by design in prototype?

Is there something that needs to be set or switched on, or otherwise, to
allow RJS to work?

I’m sure that whatever it is, it’s something obvious/simple/annoying.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs