【问题标题】:ActionController::UnknownFormat (ActionController::UnknownFormat): on destroyActionController::UnknownFormat (ActionController::UnknownFormat): 销毁
【发布时间】: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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多