问题已解决,并且不在我指责的 Rails 路由器上。我希望这对想要使用 paranoid2 gem 实现垃圾箱功能的人有所帮助。
我正在使用 paranoid2 gem(就像作为偏执狂,但用于 Rails4)来保护我的数据不被意外删除,这工作正常,我正在尝试为我的应用程序创建垃圾箱。它应该显示标记为已删除(完成)的内容,提供恢复数据的方法(待办事项),永久删除(完成)并清空整个垃圾箱(这就是问题所在)。
那些我工作正常的路线:
trash_index GET /trash(.:format) trash#index
trash GET /trash/:id(.:format) trash#show
DELETE /trash/:id(.:format) trash#destroy
我还有一个,但它不能按我的意愿工作:
all_trash_index DELETE /trash/all(.:format) trash#destroy_all
对我来说看起来不错,因为它使用 DELETE,不需要 :id 参数,并且应该路由到我的垃圾控制器.rb 中的垃圾#destroy_all:
def destroy_all
[..]
end
def destroy
[..]
end
但是当我尝试到达那里时,我最终遇到了错误:
Started DELETE "/trash/all"
Processing by TrashController#destroy_all as HTML
Parameters: {"authenticity_token"=>"..."}
Completed 500 Internal Server Error in 11ms
ArgumentError (wrong number of arguments (1 for 0)):
app/controllers/trash_controller.rb:15:in `destroy'
app/controllers/trash_controller.rb:8:in `destroy_all'
为什么我最终会采取“破坏”行动?..并且destroy和destroy_all都有错误
这是我的路线.rb:
resources :trash, only: [:index, :show, :destroy] do
match 'all', to: 'trash#destroy_all', via: :delete, on: :collection
end
问题已经解决了
是我在垃圾控制器.rb 中做的一件有趣的事:
def destroy_all
if Slide.only_deleted.map(&destroy(force: true))
[..]
解决方案:
paranoid2 提供destroy_all!这样的方法:
def destroy_all
if Slide.only_deleted.destroy_all!