Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to show server errors in Rails 2.3.5 with JSON and jQuery
    text
    copied!<p>I've got in-place editing on a page in my app (using Rails 2.3.5 and jQuery). I want to know how to display an error on the page when the update fails.</p> <p>I'm using ajax (an XMLHttpRequest) to save an update to a Comment object. The controller has an update method like this:</p> <pre><code>def update @comment = Comment.find(params[:id]) respond_to do |format| # if @comment.update_attributes!(params[:comment]) if false #deliberately forcing a fail here to see what happens format.json { render :nothing =&gt; true } else format.json { render :json =&gt; @comment.errors, :status =&gt; :unprocessable_entity } end end end </code></pre> <p>In Firebug, I can see the server returns a "422" (an appropriate validation error status code). But it's a response to an XMLHttpRequest so there is no redirect to an error page. </p> <p>I think I actually want to do this:</p> <pre><code>format.json { render :json =&gt; @comment.errors} </code></pre> <p>or maybe this:</p> <pre><code>format.json {render :json =&gt; { :status =&gt; :error, :message =&gt; "Could not be saved" }.to_json, :status =&gt; 400 } </code></pre> <p>and trigger some Javascript function that iterates through (and displays) any errors.</p> <p>I'm using a rails plugin <a href="http://github.com/janv/rest_in_place/" rel="nofollow noreferrer">REST in Place</a> to implement the in-place editing. It doesn't appear to have any callback function to handle a failure. What are my options? Can I write some Javascript to respond to a failure condition without hacking the plugin? Do I have to hack the rest_in_place plugin to handle a failure condition? Is there a better plugin (for Rails or jQuery) that handles in-place editing, including failure conditions?</p> <p><strong>UPDATE</strong></p> <p>This post from Peter Bui <a href="http://paydrotalks.com/posts/45-standard-json-response-for-rails-and-jquery" rel="nofollow noreferrer">Standard JSON Response for Rails and jQuery</a> was helpful in showing how to handle an error message from the server using <code>XMLHttpRequest.status</code>. I looked at his implementation of a blog using ajax <a href="http://github.com/paydro/talks" rel="nofollow noreferrer">paydro-talks</a>. I'm surprised at the complexity required to handle a simple error condition. Usually Rails has all the goodness baked in but it seems server errors with JSON are out of scope. Can that be?</p> <p>I also looked at grimen's <a href="http://github.com/grimen/validatious-on-rails/" rel="nofollow noreferrer">validatious-on-rails</a> which accommodates models validations when ajax XMLHttpRequest is used. It's not clear to me how I'd use it to handle the general case of a "save" failing when validations succeed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload