Until now I have always specified the format of the response for actions using a responds_to block, like so:
responds_to do |format|
format.js { render :json => @record }
end
Recently I realized that if you only support one format (as in the above example), you don’t really need that block. Is it best practice to leave it in, or remove it?
I’m going to differ with existing answers–I like to have a
responds_toblock for all of my actions. I find that, while slightly more verbose, it more clearly self-documents the action. It also makes it easy to support additional formats in the future. Edit: another advantage is it acts as a gatekeeper. Any format not declared in the block is automatically served a “406 Not Acceptable”