views:

37

answers:

2

I have a little ajax call that calls rails:

    $.ajax({
        type: "POST",
        url: '...',
        data: ({    ...
                }),
        success:    function(response, status) {
                    console.log(status);
        }
     });

In the rails controller I'm simply deleting an entry from the database, and I simply want to return if it was successful or not. What's the best way?

Should I just return JSON in respond_to? If so, what exactly would you have it contain?

A: 

When you execute your query it might be returning some code that says it executed succesfully to confirm that row was deleted. So you can return that just to make sure query was also executed successfully along with the ajax call.

sushil bharwani
Right, I know if my query was successful or not, but HOW do you suggest returning that? For example: format.json { "false" } or render :nothing => true, or ..?
99miles
A: 

Best way to signify success in this way is to put the following in your controller...

def destroy
  # ... your code ...
  respond_to do |format|
    format.json { head :ok }
  end
end
Dave Pirotte
thanks! for the record, for a failure it might be something like: format.json { render :json => @obj.errors, :status => :unprocessable_entity }
99miles