link_to_remote patterns/idiom


What's the current line of thinking wrt patterns/best practices for
remote calls and subsequent page updates?

For instance, let's say I have a simple link_to_remote call that simple
creates a new comment for a given blog post. On success, I'd like to
indicate that all was well, on failure, I'd like to give some indication
as to why.

Obviously, Rails has some nice (and by nice I mean 'programmer
friendly') abstractions for handling this type of functionality but the
myriad of options has left me in a state of paralysis.

That said, I'm seeking to understand the preferred/recommended way to
pull something like this off:

class CommentController < ApplicationController
  def create_comment
    @comment =[:comment])
      # Should I use RJS here to update the page?

      # Should I return some HTML (sans RJS) and use the prototype
helper to
      # update a DIV?
      # Would obviously look to stay consistent here.
      # Do I use RJS to update the page?
      # Shall I send back a non 2XX status code and use prototype helper

What be the prevailing pattern? Arggh.


You’re right about the myriad of options. You can either return an RJS that will update a div (with an id of comments) with the new comment contained within a partial (_comment) from your app/views/comments directory. (Complicated way)

Alternatively, you could just redirect to the item that you created a comment for which will already (I hope) have a way of iterating over the comments. You could then pass back a flash[:notice] stating the success or failure of your comment posting. (Easy way)