. Advertisement .
..3..
. Advertisement .
..4..
I am facing a problem with an error named “ActionController::UnknownFormat”, so I really need your help. I built an application in which the actions respond with HTML templates yesterday, the users feedbacked to me that when they requested the JSON version of the page, they received a 406 Not Acceptable response. Then I check my log and I found this error, looking a bit like this:
ActionController::UnknownFormat (ActionController::UnknownFormat):
As following is a piece of code to built my application:
class BlogPostsController < ApplicationController
def index
respond_to do |format|
format.html { render :index }
end
end
end
If you know of a solution, please share it with me in the comments section below.
Cause: The “ActionController::UnknownFormat” error occured because you set HTML as the only format to react to. As a result, you missed a format in a response that you wish to support.
Solution: When writing a blog post, you should consider an action where you want to reply to HTML and JSON queries to help your page be able to support an Ajax request. This is what it may look like:
When the blog post fails validations and is not saved, this error is raised. within the response block’s scope of word formatting blocks, You must use the render command. If you were to rewrite this to account for failure, it would look like this:
And now, there will be no more unintended ActionController::UnknownFormat exceptions that all formats have been declared.