Sunday, December 6, 2009

[Ruby on Rails] Error 406: Not Acceptable

You know what's "Not Acceptable"? Not having a more meaningful error message!

One probable cause for a 406 error is that you're requesting an invalid format.

For example, let's say you're requesting application/json from /model.json and you've got a block of code like:
respond_to |format|
  if @model.save
    format.html
    format.xml { render :xml => @model }
  else
    format.html
    format.xml { render :xml => @model.errors }
  end
end

That's a problem.

And you're not getting a response back telling you what the error is because the error is the server notifying your client that it can't accept the response...... infinite loop.... anyway, you've forgotten to add in something like this:
    format.json { render :json => @model.errors }

Next you'll probably get a 422, but that just means something is wrong with the data saving itself. At least in that case you'll have a decent error message to look at.

|8^D

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home