New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
invalid update json response with generated controller scaffolding #1742
Comments
Could you provide a failing test case, or a basic app reproducing the problem ? |
I created a basic app showing the problem at https://github.com/codebrew/json-issue The app is using the latests rails from head. I did a scaffolding gen of 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. |
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. |
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. |
I'm going to patch this on Rails |
I think it would be far better to finally update the controller scaffold to use |
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
respond_with(@gizmos)
end
# GET /gizmos/1
# GET /gizmos/1.json
def show
@gizmo = Gizmo.find(params[:id])
respond_with(@gizmo)
end
# GET /gizmos/new
# GET /gizmos/new.json
def new
@gizmo = Gizmo.new
respond_with(@gizmo)
end
# GET /gizmos/1/edit
def edit
@gizmo = Gizmo.find(params[:id])
respond_with(@gizmo)
end
# POST /gizmos
# POST /gizmos.json
def create
@gizmo = Gizmo.create(params[:gizmo])
respond_with(@gizmo, notice: "Gizmo was successfully created.")
end
# PUT /gizmos/1
# PUT /gizmos/1.json
def update
@gizmo = Gizmo.find(params[:id])
@gizmo.update_attributes(params[:gizmo])
respond_with(@gizmo, notice: "Gizmo was successfully updated.")
end
# DELETE /gizmos/1
# DELETE /gizmos/1.json
def destroy
@gizmo = Gizmo.find(params[:id])
@gizmo.destroy
respond_with(@gizmo, notice: "Gizmo was successfully deleted.")
end
end |
Fixed this issue, please check the pull request /cc @spastorino |
@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. |
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 |
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: * http://stackoverflow.com/questions/12407328/rails-head-ok-interpreted-as-ajaxerror * rails/rails#1742
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.
The text was updated successfully, but these errors were encountered: