I'm playing with Rails 3, and on one of my controllers I have the
following update method defined:
def update
@category = @user.categories.find(params[:id])
@category.update_attributes(params[:ecategory])
@category.save
respond_with @category
end
I'm using Jquery to interact with the controllers, and when I do an
ajax PUT, the update action is correctly executed (the fields are
updated), but the action returns an empty JSON object, which causes
the Jquery ajax function to fall into an error.
This function is used with both create and update forms, but on create
everything goes ok and it falls on the success function, while during
update, the JSON result comes blank, Jquery gets an error while trying
to parse the empty Json object and it falls on the error function.
..
The same form is used both for create and update. I use this Jquery
code to change between create and update actions (this is called on
click of the 'new category' button', or on the on click of an existing
category:
attr = $(this).data('attributes');
form = categoryForm.find('form');
form.find('input[name=_method]').remove();
I am still stuck with this. Does anyone knows if this is the expected
beavior for Rails? Should respond_with on update really return an
empty json object?
I am still stuck with this. Does anyone knows if this is the expected
beavior for Rails? Should respond_with on update really return an
empty json object?
I pretty sure the Rails behavior is as intended. Rails basically says
the save was successful by returning a 200 status code.
jQuery however, considers the request successful if it gets a 200 status
code AND some valid json content.
Personally, I like the Rails way better here.
But nonetheless, I'm currently stuck with the same problem.
An ugly (but working!) solution would be to replace your respond_with
with this
render :json => { :ok => true }
I too have same problem.
Problem: I am getting "successfully stored." alert message, but getting
"null" value in suceeding alert message. whether as expected data is
{"id" => "xx"}
the code is as follows
----------------controller code------------------------
@tmp = {}
@tmp[:id] = "xx"
respond_with do |format|
format.html{ render :json => @tmp }
format.js
end