0

The problem is solved and wasn't on the Rails router which I blamed. I hope leave this helpfull to someone who wants to implement Trash functionality with paranoid2 gem.

I'm using paranoid2 gem (like acts-as-paranoid but for Rails4) to protect my data from beeing accidently deleted which works fine and I'm trying to create Trash to my app. It should show what's marked as deleted (done), give a way to restore the data (todo), permanently delete (done) and empty whole Trash (this is the problem).

Those my routes which works fine:

trash_index GET    /trash(.:format)                       trash#index
      trash GET    /trash/:id(.:format)                   trash#show
            DELETE /trash/:id(.:format)                   trash#destroy

and the one more I have but it doesn't work as I want:

all_trash_index DELETE /trash/all(.:format)                   trash#destroy_all

Looks fine for me as it uses DELETE, doesn't require :id parameter and should route to trash#destroy_all which I have in my trash_controller.rb:

    def destroy_all
        [..]
    end

    def destroy
        [..]
    end

But when I try to get there I end up with error:

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'

why I end up in 'destroy' action? ..and got errors for both destroy and destroy_all

This is my routes.rb:

resources :trash, only: [:index, :show, :destroy] do
    match 'all', to: 'trash#destroy_all', via: :delete, on: :collection
end

THE PROBLEM IS SOLVED

was by funny thing I did in trash_controller.rb:

def destroy_all
  if Slide.only_deleted.map(&destroy(force: true))
  [..]

solution:

paranoid2 provides destroy_all! method so:

def destroy_all
  if Slide.only_deleted.destroy_all!
pawel7318
  • 3,383
  • 2
  • 28
  • 44

1 Answers1

0

Stack trace indicates you just call destroy function from destroy_all. Routing have nothing to do with this problem.

Sergey Moiseev
  • 2,953
  • 2
  • 24
  • 28
  • you're right ! The problem is in the first line of my destroy_all action: if Slide.only_deleted.map(&destroy(force: true)) – pawel7318 Mar 18 '14 at 16:46
  • Funny but typical. How about accept my answer? Or may be you need more help? :) – Sergey Moiseev Mar 18 '14 at 16:48
  • For example, Slide.only_deleted.[destroy_all](http://apidock.com/rails/ActiveRecord/Base/destroy_all/class) are something you looking for. Map not needed in this case. – Sergey Moiseev Mar 18 '14 at 16:51
  • I'll definitly do. I'll appreciate if you could tell me how to fix this. My destroy def is: if @trashed_slide.destroy(force: true) [..] where @trashed_slide = Slide.only_deleted.find(params[:id]) which works fine and for destroy_all I would like to use destroy(force: true) for Slide.only_deleted but Slide.only_deleted.destroy(force: true) gives me "Unknown key: force" error. – pawel7318 Mar 18 '14 at 16:54
  • See my last comment. You just need to call destroy_all for collection. And what are you trying to achieve with force option? – Sergey Moiseev Mar 18 '14 at 16:58
  • I can't use .destroy_all exctly because this force option which is crucial. This is what paranoid2 gem provides. It overwrites destroy method with it's own - with force: true it works as original destroy but without it only set deleted_at value for the data. Unfortunately it doesn't provide .destroy_all with force parameter. – pawel7318 Mar 18 '14 at 17:05
  • actually maybe paranoid2 has something usefull: module ClassMethods def paranoid? ; true ; end def destroy_all!(conditions = nil) with_paranoid(force: true) do destroy_all(conditions) end end – pawel7318 Mar 18 '14 at 17:09
  • I suggest you to update your question with update about our findings in this discussion. – Sergey Moiseev Mar 18 '14 at 17:11
  • I just did. Thank you. – pawel7318 Mar 18 '14 at 17:35