Backend delete method failing to delete

Viewed 43

I have lately been having trouble with some code I've been writing, specifically with the portions for deleting an User's Booking of an Hotel. When I click the toggleable button on the Hotel component, it will trigger one of two different functions, depending if the logged-in User has already booked at the Hotel (which triggers my deleteBooking function) or not (which triggers my postBooking function). For some reason, (I think) my set_booking method isn't working. Checking my http://localhost:3001/users/${currentUser.id}/bookings URL, all of the bookings are there (my POST request is working, so no problems there), but my http://localhost:3001/users/${currentUser.id}/bookings/${bookingId} URL, the only thing I get returned is null, and my console keeps returning only DELETE http://localhost:3001/users/8/bookings/1 500 (Internal Server Error). My version of postgresql is 1.1.

I've already tried using debugger and byebug along the entire flow of this portion of my code (and immediately after it), as well as changing the variables being used.

bookings_controller.rb:

skip_before_action :authorize, only: [:create, :destroy]

def destroy
        # byebug
        set_booking
        byebug
        render json: @booking
        byebug 
        @booking.destroy
        byebug
    end

private
def set_booking
    @booking = Booking.find(params[:id])
    # render json: @booking
    byebug
end

def booking_params
    params.require(:booking).permit(:hotel_id, :user_id, :id)
end

Hotel.jsx:

function toggleBooking(e){
console.log("e.target: ", e.target.id);
if(booked === true){
  const booking = bookings.find(booking => {return booking.hotel.id == e.target.id});
  console.log("Booking.id: ", booking.id);
  deleteBookings(booking.id);
  setBooked(!booked);
} else if(booked === false){
  postBookings();
  setBooked(!booked);
}

console.log("Your Bookings: ", bookings);}




function deleteBookings(bookingId){
fetch(`http://localhost:3001/users/${currentUser.id}/bookings/${bookingId}`, {
  method: "DELETE"
})
.then((r) => {
  if(r.ok)onDeleteBookings(bookings.id);
})}

function onDeleteBookings(deletedBooking){
const updatedBookings = bookings.filter((booking) => booking.id !== deletedBooking.id);
setBookings(updatedBookings);}

Does anyone know how to solve this issue? And will you need the repo link?

0 Answers
Related