Forum: Ruby on Rails Trouble with firefox and AJAX

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C91f4365ef2df4be3c68269d044174b0?d=identicon&s=25 Andrew Durdin (Guest)
on 2006-05-03 19:09
(Received via mailing list)
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 ====

<!-- there's a layout used that imports prototype -->

<div id='posts'>
	<%= render :partial => "post", :collection => @posts %>
</div>

<br />

<!-- insert the edit form at the bottom of the post list -->
<%= link_to_remote 'Reply', :update=>'posts', :position=>'bottom',
	:url=>{ :action=>'post_edit', :topic=>@topic } %>

==== _post.rhtml ====

<div class='post' id='post_<%= post.id %>'>
        <span class='date'><%= post.posted_time %></span>
        <span class='title'><%= post.title %></span>
        <span class='post_text'><%= post.post %></span>
    </div>
</div>

==== _post_form.rhtml ====

<% post = post_form %>
<div id='post_edit'>
    <!-- there should be a whole form here, but it's irrelevant -->

    <!-- 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");' %>

</div>

==== end files ====
This topic is locked and can not be replied to.