我知道我可以通过编辑config/routes.rb在 http://localhost:3000/students 上添加新的DELETE方法:
resources :students
delete '/students',:to => 'students#destroymany'
注意:它与 DELETE http://localhost:3000/students/1 不同,因为默认 DELETE 仅支持删除 1 名学生,而我也想支持 DELETE on http://localhost:3000/students/(没有特定的学生证(
但它有一个缺点:我需要重复输入"学生"。是否有任何语法可以将路由语句更改为
resources :students do
xxx
end
但是和开始版本有相同的效果吗?我试过了:
resources :students do
delete '',:to => 'students#destroymany'
end
但发生错误:
No route matches [DELETE] "/students"
因为它将路由添加到
/students/:student_id
而不是
/students
它与 DELETE http://localhost:3000/students/1 不同,因为 默认 DELETE 仅支持删除 1 名学生,而我也想要 支持 http://localhost:3000/students/上的删除(没有特定的 学生证(
好吧,然后将其添加到集合中
resources :students do
delete '', :to => 'students#destroymany', on: :collection
end
这将给出以下路线
DELETE /students(.:format) students#destroymany
因此,您可以使用method: :delete
调用/students
您可以改为执行此操作,以实现您要完成的任务:
resource :students do
collection do
delete '', to: 'students#destroymany'
end
end
利用奇异资源。单一资源将为您提供删除没有任何ID的路由
resource :students
删除/学生 学生#销毁 请参考单数资源