本文介绍了尝试通过 link_to 使用 Rails 4 删除表中的所有记录时出现路由错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Rails 4,并尝试使用 link_to 帮助程序删除所有评估.相关代码如下:

I am using Rails 4, and am trying to delete all the assessments using a link_to helper. The pertinent code is below:

routes.rb

   resources :assessments do
     collection do
       delete :remove_all
     end
   end

评估 index.html.erb

 <p>To delete all assessments in one swoop, click <%= link_to 'Remove ALL Assessments', remove_all_assessments_path, method: :destroy, data: { confirm: 'Are you sure?' } %>

assesments_controller.rb

  def remove_all
    @assessments = Assessment.all
    @assessments.each do |assessment|
        assessment.destroy(assessment.id)
     end
    flash[:notice] = "All assessments have been deleted."
    redirect_to assessments_url
  end

我运行 rake routes

Prefix                 Verb   URI Pattern                       Controller#Action
remove_all_assessments DELETE /assessments/remove_all(.:format) assessments#remove_all

为链接生成的 HTML 源代码:

<p>To delete all assessments in one swoop, click <a data-confirm="Are you sure?" data-method="destroy" href="/assessments/remove_all" rel="nofollow">Remove ALL Assessments</a>

当我单击删除所有评估"链接时,我希望在 AssessmentsController 中运行 remove_all 操作,并删除@assessments 中的所有评估,然后将我重定向到评估 URL.但是,当我单击删除所有评估"链接时,我被带到了 url:http://localhost:3000/assessments/remove_all 并出现错误 No route matching [POST] "/assessments/remove_all"

When I click on the 'Remove All Assessments' link, I expect to have the remove_all action run in the AssessmentsController, and delete all the assessments in @assessments, then redirect me to the assessments_url. However, when I click on the 'Remove ALL Assessments', link, I am brought to the url: http://localhost:3000/assessments/remove_all with the error No route matches [POST] "/assessments/remove_all"

是什么?

推荐答案

应该是<p>要一次性删除所有评估,请单击 <%= link_to 'Remove ALL Assessments', remove_all_assessments_path, method: :delete, data: { confirm: 'Are you sure?'} %>

这篇关于尝试通过 link_to 使用 Rails 4 删除表中的所有记录时出现路由错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 05:04