Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simply returning success or failure from ajax call in rails

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?

like image 558
99miles Avatar asked Sep 25 '10 05:09

99miles


3 Answers

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
like image 147
Dave Pirotte Avatar answered Nov 15 '22 07:11

Dave Pirotte


try this it's working for me

def destroy 
  ...        
  render json: {}, status: 200
end
like image 44
El Fadel Anas Avatar answered Nov 15 '22 08:11

El Fadel Anas


I found this shorter way to do the job:

def destroy
  # ... your code ...
  head :ok # this will return HTTP 200 OK to jQuery!
end
like image 2
Eugene Avatar answered Nov 15 '22 09:11

Eugene