rails 7 ActionController::UnknownFormat 在 ManagersController#destroy has_one 关联销毁操作中出现问题

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

我有一个简单的 rails 应用程序,其中有一个项目 has_one 经理。 除@manager.destroy 操作外,所有操作都按预期工作。该记录已从数据库中成功删除,但我在 ManagersController#destroy 错误中收到 ActionController::UnknownFormat。 下面是一些关键代码摘录。

非常感谢您的帮助,谢谢!!

型号

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

对于这个例子 manager_id = 11

销毁时出现错误消息 -- 注释记录已成功删除。

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

提取的源代码(大约第 55 行):# -> 见指向箭头

    @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)

我通过将“redirect_to ...”移出 respond_to |格式| 设法避免了 ActionController::UnknownFormat 错误区块如下:

def 销毁 @manager.destroy redirect_to @project,通知:“管理器已成功销毁。”

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

结束