Search code examples
ruby-on-railsarraysactionchecked

Delete all checked items rails


i have array tasks[] with items from form

<%= check_box_tag "tasks[]", task.id %>

I see them in console

Processing by TasksController#destroy as HTML
  Parameters: {"utf8"=>"?", "authenticity_token"=>"qo6JqGn0a1Yck1j67taz7kEu/ENBrwLg0xs4HbmAehNq7yMVB3llJWYgZvRNrWKPrZqYJtZIaS89EIBFIyDZTA==", "tasks"=>["7", "8"], "commit"=>"Trash All Checked", "id"=>"delete_all"}

But can't delete, action dont work

def delete_all
    Task.where(id: params[:id]).destroy_all
    redirect_to action: "index"
  end

Solution

  • Your IDs are present in params[:tasks], so you have to write something like that:

    def delete_all
      Task.where(id: params[:tasks]).destroy_all
    
      redirect_to action: "index"
    end
    

    Anyway, I'd recommed to change tasks to tasks_ids in the view and the controller. It's a more meaningful name.