Skip to content


Subversion checkout URL

You can clone with
Download ZIP


invalid update json response with generated controller scaffolding #1742

codebrew opened this Issue · 17 comments

10 participants


The generated controller update method responds to successful json requests with head :ok. This renders a response with an empty body, which jquery fails to parse. All successful update request made by jquery will end up executing an error callback. Rendering of an empty json object {} fixes this error.


Could you provide a failing test case, or a basic app reproducing the problem ?


I created a basic app showing the problem at

The app is using the latests rails from head. I did a scaffolding gen of rails g scaffolding Post title:string content:string then I added some javascript in app/assets/javascripts/ to make an update 'PUT' request to the server.

start up the server go to localhost:3000/posts, create a new post model, and in the table there will be a link called 'Ajax Update' it will make a successful ajax put request but the response body will be empty which causes jquery fails to parse and will execute the error callback.

@codebrew codebrew closed this
@codebrew codebrew reopened this

This seems to be something that jQuery should take care of. Returning a 200 with an empty body should be a completely reasonable response if there's nothing to return. If we need to patch that temporarily in the jquery-rails adapter, then so be it. But it seems silly for us to return an empty json object.

@dhh dhh closed this

If we need to patch this in the jquery-rails adapter, be sure to open a ticket in the jquery-ujs issues and I can take a look.

@spastorino spastorino reopened this

I'm going to patch this on Rails


I think it would be far better to finally update the controller scaffold to use respond_with which already responds with {} for updates.


If you're making a patch to the controller scaffold also update the destroy method, which has the same issue as update.


All of the controller methods should be updated. I know there was hesitation in the past because using scaffold isn't the preferred way to develop your application.

class GizmosController < ApplicationController
  respond_to :html, :json

  # GET /gizmos
  # GET /gizmos.json
  def index
    @gizmos = Gizmo.all


  # GET /gizmos/1
  # GET /gizmos/1.json
  def show
    @gizmo = Gizmo.find(params[:id])


  # GET /gizmos/new
  # GET /gizmos/new.json
  def new
    @gizmo =


  # GET /gizmos/1/edit
  def edit
    @gizmo = Gizmo.find(params[:id])


  # POST /gizmos
  # POST /gizmos.json
  def create
    @gizmo = Gizmo.create(params[:gizmo])

    respond_with(@gizmo, notice: "Gizmo was successfully created.")

  # PUT /gizmos/1
  # PUT /gizmos/1.json
  def update
    @gizmo = Gizmo.find(params[:id])

    respond_with(@gizmo, notice: "Gizmo was successfully updated.")

  # DELETE /gizmos/1
  # DELETE /gizmos/1.json
  def destroy
    @gizmo = Gizmo.find(params[:id])

    respond_with(@gizmo, notice: "Gizmo was successfully deleted.")

Fixed this issue, please check the pull request

/cc @spastorino


@dhh @codebrew

the problem is that you're calling head :ok which renders " " as the body, not "". I'm sure why rails returns a 1-character body on any head response, but it might have something to do with HTTP standards.

@spastorino spastorino was assigned

@OhaiBBQ The single character body is sent to workaround a bug in older versions of Safari. Safari ignored headers if the body has zero length.


Alright, that makes sense. Thanks.


I'm closing this since we pulled in @mitijain123's pull request.

@tenderlove tenderlove closed this
@spastorino spastorino reopened this

This wasn't fixed yet


thanks /cc tenderlove .

/cc spastorino let me know if you have any idea in mind, I like to implement that, if you give me a chance.


@mitijain123 we should do head :success, and make that return 204


We fixed that here aef62c4
If you're hitting the issue change head :ok with head :no_content

@spastorino spastorino closed this
@merqlove merqlove referenced this issue from a commit in cnsa/bigbluebutton_rails
@daronco daronco Modify empty json responses to prevent errors with jquery
When using 'head :ok' to respond with a json, the body is sent as ' ' (an empty
space). This is interpreted by jquery as an error even if the response code is 200.
The way we're doing it now sends a string in the body and is interpreted as a
successful response. See more about it at:
* rails/rails#1742
@crivera crivera referenced this issue in AFNetworking/AFNetworking

Can't override AFURLResponseSerialization encoding #2478

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.