ActionController::UnknownFormat (ActionController::UnknownFormat): 销毁

Posted

技术标签:

【中文标题】ActionController::UnknownFormat (ActionController::UnknownFormat): 销毁【英文标题】:ActionController::UnknownFormat (ActionController::UnknownFormat): on destroy 【发布时间】:2022-01-14 21:42:09 【问题描述】:

我正在尝试销毁记录,我有 3 个用于列出记录的菜单。所以在销毁记录后,它应该重定向到我单击销毁方法的同一菜单。

但是当我尝试它时,我得到了这个错误。

 ActionController::UnknownFormat (ActionController::UnknownFormat): app/controllers/my_controller.rb:56:in `destroy'

删除链接

 <a href="<%=my_path(data,:page_name=>params[:action])%>" class="btn"" data-placement="bottom" title="Delete" data-method="delete" data-confirm="Are you sure?">
  <i class="fa fa-trash-o" aria-hidden="true"></i>
 </a>

销毁方法

 def destroy
   @page.destroy
   respond_to do |format|
     if params[:page_name] == "first"
       format.html  redirect_to first_home_index_path, notice: 'Url was successfully destroyed.' 
     elsif  params[:page_name] == "second"
       format.html  redirect_to second_home_index_path, notice: 'Url was successfully destroyed.' 
     elsif  params[:page_name] == "third"
       format.html  redirect_to third_home_index_path, notice: 'Url was successfully destroyed.' 
     end
     format.json  head :no_content 
   end
 end

【问题讨论】:

【参考方案1】:

respond_to 中添加条件应该发生在format 块内部,而不是外部。

 def destroy
  @page.destroy
  respond_to do |format|
   format.html do 
     if params[:page_name] == "first"
       redirect_to first_home_index_path, notice: 'Url was successfully destroyed.'
     elsif ....
     end
   end
   format.json  head :no_content 
 end
end

【讨论】:

以上是关于ActionController::UnknownFormat (ActionController::UnknownFormat): 销毁的主要内容,如果未能解决你的问题,请参考以下文章