ruby-on-railsdestroyactioncontrollerhas-one

rails 7 ActionController::UnknownFormat in ManagersController#destroy Problem with has_one association destroy action


I have a simple rails app where a Project has_one Manager. All actions works as expected except for the @manager.destroy action. The record is deleted successfully from the database but I get an ActionController::UnknownFormat in ManagersController#destroy error. Some of the key code extracts are below.

Your help is greatly appreciated, thank you!!

Model

class Project < ApplicationRecord
  has_one :manager, dependent: :destroy
end
class Manager < ApplicationRecord
  belongs_to :project, inverse_of: :manager
end

managers_controller.rb

  before_action :set_manager, only: %i[ show edit update destroy ]


def set_project
  @project = Project.find(params[:project_id])
end
def set_manager
  @manager = @project.manager
end



def destroy
  @manager.destroy
    respond_to do |format|
      format.html { redirect_to @project, notice: "Manager was successfully destroyed." }
      format.json { head :no_content }
  end
end

for this example manager_id = 11

error message on destroy -- note record is successfully deleted.

ActionController::UnknownFormat in ManagersController#destroy ActionController::UnknownFormat

Extracted source (around line #55): # -> see pointing arrow

    @manager.destroy

    respond_to do |format|
->  format.html { redirect_to @project, notice: "Manager was successfully destroyed." }
      format.json { head :no_content }
    end

Parameters:

{"_method"=>"delete", "authenticity_token"=>"[FILTERED]", "project_id"=>"1", "format"=>"11"}

Note the above where format has a value of 11 (?the manager's ID)


Solution

  • I managed to avoid the ActionController::UnknownFormat error by moving the "redirect_to ..." out of the respond_to |format| block as follows:

    def destroy @manager.destroy redirect_to @project, notice: "Manager was successfully destroyed."

    # respond_to do |format|
    #   format.html { redirect_to @project, notice: "Manager was successfully destroyed." }
    #   format.json { head :no_content }
    # end
    

    end