Search code examples
ruby-on-railscheckboxmailboxerrecycle-bin

Ruby on rails. Mailboxer, move multiple conversations to trash. Undefined method for #<Array


I am struggling with sending multiple items to the trash folder using checkboxes. I get an

undefined method `move_to_trash' for #<Array:0x007...etc...

move_to_trash works fine on single conversations.

I have a checkbox next to each conversation rendered by a partial, and a button to delete all checked conversations.

Anyway, my conversations controller:

def trash_multiple
    @convo = mailbox.conversations.find(params[:trash_id])
    @convo.move_to_trash(current_user)
    redirect_to mailbox_inbox_path
  end

The checkbox which is in a partial, placed next to each conversation:

<%= check_box_tag "trash_id[]", conversation.id %>

The id's are correct.

The form:

<div class="message-cont">
  <div class="col-md-8">
    <%= form_tag trash_multiple_conversations_path, method: :post do %>
    <%= submit_tag "Trash selected" %>
      <div class="panel-body">
         <% if is_conversation %>
            <%= render 'conversations/form' %>
        <% else %>
        <div class="msg-cnter">
        <%= render partial: 'conversations/conversation', collection: messages %>
        </div>
        <% end %>
        <% end %>
      </div>
  </div>
</div>

And my routes:

resources :conversations do
    member do
      post :reply
      post :trash
      post :untrash
    end
    collection do
      get :trashbin
      post :empty_trash
      post :trash_multiple
    end
  end

Any hints on getting this to work for an array would be greatly appreciated, thanks.

SOLUTION:

changing controller to:

def trash_multiple
    params[:trash_id].each do |element|
    @convo = mailbox.conversations.find(element)
    @convo.move_to_trash(current_user)
    end
    redirect_to mailbox_inbox_path
  end

as stated by @wpp has fixed this.


Solution

  • move_to_trash works fine on single conversations.

    My guess is that you want to invoke the move_to_trash method on each element of the array:

    array.each do |element|
      element.move_to_trash
    end
    

    or shorter:

    array.map(&:move_to_trash)