Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 destroy multiple record through check boxes

I have issue with multiple delete using checkboxes. when i m deleting multiple records it get s ids for checkboxes but it is passing a method name as parameter and shows me error.

here is my code,

  **In my Controller method :**
  def destroy
    @ticket = current_user.tickets.find(params[:ticket_ids])
    @ticket.destroy

    respond_to do |format|
     format.html { redirect_to tickets_url }
     format.json { head :no_content }
    end
  end    


 def destroy_multiple
    Ticket.destroy(params[:tickets])

    respond_to do |format|
    format.html { redirect_to tickets_path }
    format.json { head :no_content }
  end
end

**In my index.html.erb**

<%= form_tag destroy_multiple_tickets_path, method: :delete do %>   
.
.
<td class="table-icon">
  <%= check_box_tag "ticket_ids[]", ticket.id %>
</td>
.
.
<%= submit_tag "Delete selected" %>

**In routes.rb**

resources :tickets do
  collection do
    delete 'destroy_multiple'
  end
end

it shows me this error ::::

 Couldn't find Ticket with id=destroy_multiple [WHERE "tickets"."user_id" = 1]

passes arguement ::::

  {"utf8"=>"✓",
  "_method"=>"delete",
  "authenticity_token"=>"yHeRR49ApB/xGq1jzMTdzvix/TJt6Ysz88nuBEotHec=",
  "ticket_ids"=>["11",
  "12"],
  "commit"=>"Delete selected",
  "id"=>"destroy_multiple"}
like image 789
SSR Avatar asked Apr 30 '13 13:04

SSR


2 Answers

Step:1 In routes.rb

resources :tickets do
  collection do
    delete 'destroy_multiple'
  end
end

Step:2 In _form.html.erb

<%= form_tag destroy_multiple_tickets_path, method: :delete do %>   
   <td class="table-icon">
     <%= check_box_tag "ticket_ids[]", ticket.id %>
   </td>
  <%= submit_tag "Delete selected" %>
<%end%>

Stpe:3 In Controller

def destroy_multiple
  Ticket.destroy(params[:tickets])
    respond_to do |format|
      format.html { redirect_to tickets_path }
      format.json { head :no_content }
    end
end
like image 145
KKB Avatar answered Oct 21 '22 16:10

KKB


do

Ticket.destroy(array_of_ids)
like image 39
prasad.surase Avatar answered Oct 21 '22 17:10

prasad.surase