2013-04-30 5 views
7

मुझे चेकबॉक्स का उपयोग करके एकाधिक हटाएं जारी हैं। जब मैं एकाधिक रिकॉर्ड हटा रहा हूं तो इसे चेकबॉक्स के लिए एस आईडी मिलती है लेकिन यह पैरामीटर के रूप में एक विधि नाम गुजर रही है और मुझे त्रुटि दिखाती है।रेल 3 चेक बॉक्स के माध्यम से एकाधिक रिकॉर्ड को नष्ट कर देता है

यहाँ

मेरी कोड है,

**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 

यह मुझे इस त्रुटि ::::

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

गुजरता तर्क ::::

{"utf8"=>"✓", 
    "_method"=>"delete", 
    "authenticity_token"=>"yHeRR49ApB/xGq1jzMTdzvix/TJt6Ysz88nuBEotHec=", 
    "ticket_ids"=>["11", 
    "12"], 
    "commit"=>"Delete selected", 
    "id"=>"destroy_multiple"} 

उत्तर

3

कर

Ticket.destroy(array_of_ids) 
2

नमस्ते, अपने नियंत्रक अद्यतन से पता चलता इसी प्रकार कोड ..

def destroy_multiple 
@tickets = Ticket.find(params[:ticket_ids]) 
@tickets.each do |ticket| 
ticket.destroy 
end 
end 
+0

जहां यू "@ticketsts" इस का उपयोग ??? यह सुधार और सही कोड है .... "टिकट.destroy (पैराम्स [: टिकट_आईडीएस])" – SSR

+0

यह एक टाइपो है, मैंने इसे @SSR को सही किया है – Radhakrishna

2

इस

Ticket.where(:id => params[:ticket_ids]).destroy_all 
4

चरण का प्रयास करें: 1 routes.rb

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

चरण में: 2 _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 नियंत्रक में

def destroy_multiple 
    Ticket.destroy(params[:tickets]) 
    respond_to do |format| 
     format.html { redirect_to tickets_path } 
     format.json { head :no_content } 
    end 
end 
संबंधित मुद्दे