mercredi 19 août 2015

Rails 4 - destroy action deletes wrong record

I am doing an ajax request to rails passing in the data an id.

here is the ajax

function delete_availability(id) {
    var id = id;

    $.ajax({
      type: "DELETE",
      url: "/events/" + id,
      statusCode: {

        200: function() {
          //alert("200");
        },
        202: function() {
          //alert("202");
        }
      },
      success: function(data) {

        console.log('availability deleted');

      },
      error: function(xhr) {
        alert("The error code is: " + xhr.statusText);
      }
    });
  }

my destroy action

def destroy

    @event = Event.find_by(params[:id]);

    respond_to do |format|
      if @event.destroy
        format.json {
          render json: {}
        }
      end
    end
  end

my event model has nothing in it

class Event < ActiveRecord::Base

end

the problem is even though rails receives correct id, when it goes for destroying, it changes id and destroys the next one.

here is the rails log:

Processing by EventsController#destroy as */*
  Parameters: {"id"=>"66"}
  Event Load (0.1ms)  SELECT  "events".* FROM "events" WHERE (66) LIMIT 1
   (0.0ms)  begin transaction
  SQL (0.2ms)  DELETE FROM "events" WHERE "events"."id" = ?  [["id", 65]]
   (2.4ms)  commit transaction
Completed 200 OK in 6ms (Views: 0.1ms | ActiveRecord: 2.8ms)

anyone knows why?

Aucun commentaire:

Enregistrer un commentaire