Rails 422: The change you wanted was rejected.

July 1, 2009
 

Hey everyone,

I’ve been using routes in my Ruby on Rails to shorten URLs for users who share their meals on Twitter — http://mealstrom.com/379 is a lot shorter than http://www.mealstrom.com/meal/show/379. Producing nice-looking URLs is as simple as adding the following line to my routes.rb:

map.connect ':id', :controller => "meal", :action => "show",
                          :requirements => { :id => (/[0-9]+/) }

This setup worked smoothly until I submitted the 422nd meal. I went to check it out, and to my great shock and consternation, encountered the following error message:

error422

I set to work, and discovered that the Internet was, for once, no real help — no one seemed to have encountered a similar problem. A fair amount of investigation culminated in the discovery that the Rails server wasn’t handling the request at all.

It turns out that 422 is one of the three HTTP error codes that Rails provides special support for (the others are 404 and 500). When a request comes in for any of those numbers in the root of a Rails site, no application logic gets called — the 422.html file is just down like any file in the public directory. (Except not really — I tried deleting that file, and Rails still sent the error message down. It appears to be hard-coded deeper than that.)

This makes a lot of sense now, but having discovered it for 422 rather than the earlier 404 made it a lot harder to figure out. (422 is a much less common error code.) In the end I didn’t actually change anything — I annotated the Twitter and Facebook posts for the meal, and when the time came I created a dummy record for meal 500 to prevent this from happening again. There aren’t any numerically-higher hard-coded error messages.

Hopefully if anyone else makes the configuration decisions I did and encounters this fairly obscure problem, this post will be of use to them. I know it would have been to me.

Cheers,

Alex

One Response to “Rails 422: The change you wanted was rejected.”

  1. [...] Read this entry on blog.twoalex.com Categories: tech, website Tags: Comments (0) Trackbacks (0) Leave a comment Trackback [...]

Leave a Reply