Trouble with firefox and AJAX


#1

I’ve been creating my own message board in rails as a learning
exercise. I’m currently trying to implement posting using AJAX, so the
topic page as a whole doesn’t need reloading.

What I’ve written works as epected in IE6 and Safari, but the AJAX
call fails silently in Firefox 1.5 – although the server has logged
the request, the “success” callback event seems never to trigger, and
there are no exceptions in the javascript console, and none in the
script/server output or logs.

I expect I’m doing something wrong here, but I’m at my wits end trying
to figure out what it is! If anyone can help me, I’d be very
grateful.

Below are what I believe are the relevant file excerpts. What I’m
trying to achieve is this: when the user clicks “reply”, a form
appears at the bottom of the list of posts for them to use. If they
click “edit” on a post, the form is inserted before the post, and the
post is then deleted.

After typing their post, if they choose ‘save’, the saved post is
inserted before the form, then the form is deleted. If they choose
‘cancel’, the original post (or nothing if they’re making a new post)
is inserted before the form, then the form is deleted.

For debugging, I’ve removed the code to delete the posts and forms
from the page; I’ve also concentrated here on just the ‘cancel’ link,
as that shows the failure most simply. I’ve put debugging alerts on it
to indicate the state. When I click ‘reply’, the form (i.e. the
“cancel” link) is inserted as expected; when I then click on ‘cancel’,
Safari and IE seem to work fine, while Firefox pops up the ‘before’
and ‘after’ messages, and then nothing. The server logs show that the
post_view request is received and processed, so I’m stumped as to why
this fails. Any ideas?

Cheers,

Andrew

==== forum_controller.rb ====

class ForumController < ApplicationController
def viewtopic
topic_id = params[:id].to_i
begin
@topic = Topic.find(topic_id)
rescue ActiveRecord::RecordNotFound
flash[:notice] = “This topic does not exist”
redirect_to :action=>:index
return
end

    @posts = @topic.posts
end


# Post handling

def post_view
    if params[:id]
        @post = Post.find(params[:id])
        render :partial => "post", :object => @post
    else
        # Do nothing for an unsaved post
        render :nothing => true
    end
end

def post_edit
    if params[:save]
        # Create and save a Post from the form details
        @post = Post.new params[:post]

        if @post.save
            render :partial => "post", :object => @post
        end
    else
        # Create a new Post or load one--to edit
        if params[:id]
            @post = Post.find(params[:id])
        else
            @post = Post.new :topic_id=>params[:topic]
        end
    end

    render :partial => "post_form", :object => @post
end

end

==== viewtopic.rhtml ====

<%= render :partial => "post", :collection => @posts %>

<%= link_to_remote ‘Reply’, :update=>‘posts’, :position=>‘bottom’,
:url=>{ :action=>‘post_edit’, :topic=>@topic } %>

==== _post.rhtml ====

<%= post.posted_time %> <%= post.title %> <%= post.post %>

==== _post_form.rhtml ====

<% post = post_form %>

<!-- cancelling should insert the post view (which is nothing for a
    cancelled new post) before the edit form -->
	<%= link_to_remote 'Cancel', :update=>'post_edit', 

:position=>‘before’,
:url=>{ :action=>‘post_view’, :id=>post.id, :topic=>@topic },
:before=>‘alert(“before”);’,
:after=>‘alert(“after”);’,
:complete=>‘alert(“complete”);’ %>

==== end files ====