Forum: Ruby on Rails Ajax commenting - PLEASE HELP!!!!

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.
ryan (Guest)
on 2006-05-06 10:26
I'm trying to post comments using Ajax.  It seems that I have an
infinite loop somewhere, because it hangs on the return from the Ajax
call.  The comment does get saved to the DB, but I have to refresh the
page for it to show.

The reason I know it's some sort of infinite loop, is because I'm
showing a "loading.gif" image, and it never goes away.  Plus, if the
comment doesn't save, I'm returning an error message.  I've posted the
code for the VIEW and CONTROLLER below.  Please HELP!

#VIEW
<div id="cmnts-form">
  <div id="add-comment">Have something to say?</div>
    <%= form_remote_tag(
          :url => {:controller => 'journal',
                   :action => 'comments',
                   :id => entry.id},
          :update => {:success => "new-comment"},
          :loading => "Element.show('loading');",
          :success => "Element.hide('loading');",
          :complete => "clearText();",
          :position => "bottom"
          ) %>

     <p>Name:<br/>
     <%= text_field "comment", "name", :size => "20" %></p>

     <p>Remark:<br/>
     <%= text_area "comment", "entry", :rows => "7", :cols => "35"%></p>

     <p><input type="submit" name="submit" id="form-submit-button"
value="Post Remark" class="btn" />
<img id="loading" src="../images/loading.gif" alt="" style="display:
none;" /></p>

    <%= end_form_tag %>
  </div>

#CONTROLLER
def comments
    post = Post.find(params[:id])
    @comment = Comment.new(params[:comment])
    post.comments << @comment
    post.save
    if @comment.save then
      @comment_count = Comment.count("post_id=#{params[:id]}")
      render_without_layout
    else
      render :text => "Error saving comment"
    end
  end
Alex W. (Guest)
on 2006-05-07 03:09
ryan wrote:
> I'm trying to post comments using Ajax.  It seems that I have an
> infinite loop somewhere, because it hangs on the return from the Ajax
> call.  The comment does get saved to the DB, but I have to refresh the
> page for it to show.

My guess is that your getting hung up on a javascript syntax error.  The
loading gif never goes away because the javascript hits an error half
way and never finishes executing.

First, I recommend that you use the firebug extension for firefox, as it
lets you inpect the query, and response of AJAX calls super easily.

Second, turn on RJS error reporting by adding the following to
config/environments/development.rb

  # RJS Error reporting
  config.action_view.debug_rjs = true

That should make anything that goes wrong popup in an alert box.
This topic is locked and can not be replied to.