0
votes

Good night people. I'm new to Rails and I'm using Google Translate to post here.

A doubt.

I have a table that uses polymorphism to bookmark "AtaPublic" or "AtaObject".

In my destroy, I don't want to delete a "favorite", by the id of favorites.id directly, I would like something like this to come from the front: route: {{URL}}/favorites/ata_public/2 that the id of "AtaPublic" or "AtaObject" came to perform the destroy of the favorite, and not the day of the favorite_id

The question is, what should my destroy controller look like? and my params set?

FavoritesController That way it deletes by the ID of the favorite.

  def destroy
    if @favorite.destroy
      render json: {success: I18n.t("favorite.destroy.success_message")}, status: 200
    else
      render json: ErrorMessages.new(error: I18n.t("favorite.destroy.error_message"))
        .call, status: 422
    end
  end

private
  def parent_ata_public
    return AtaPublic.find_by_id(params[:ata_public_id]) if params[:ata_public_id]
  end

  def parent_ata_object
    return AtaObject.find_by_id(params[:ata_object_id]) if params[:ata_object_id]
  end

  def set_favorite
    begin
      @favorite = Favorite.where(user: @current_user).find(parent_ata_public)
    rescue
      render json: ErrorMessages.new(error: I18n.t("favorite.not_found.error_message")).call, status: 404
    end
  end

how would it be to delete by the same id in the route? route: {{URL}}/favorites/ata_public/2

img database: https://i.stack.imgur.com/qX9uD.png