2013-04-30 22 views
7

Tôi gặp vấn đề với nhiều lần xóa bằng các hộp kiểm. khi tôi xóa nhiều bản ghi, nó sẽ nhận được các id cho các hộp kiểm nhưng nó đang chuyển một tên phương thức làm tham số và hiển thị lỗi cho tôi.Rails 3 hủy nhiều bản ghi thông qua các hộp kiểm tra

đây là mã của tôi,

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

nó cho thấy tôi lỗi này ::::

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

đèo đối số ::::

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

Trả lời

3

làm

Ticket.destroy(array_of_ids) 
2

Hi, Cập nhật điều khiển của bạn mã tương tự như vậy ..

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

nơi bạn sử dụng "@ticketsts" này ??? đây là hiệu chỉnh và mã hoàn hảo .... "Ticket.destroy (params [: ticket_ids])" – SSR

+0

của nó một lỗi đánh máy, tôi đã sửa chữa nó @SSR – Radhakrishna

2

Hãy thử điều này

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

Bước: 1 Trong routes.rb

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

Bước 2: Trong _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 Trong Bộ điều khiển

def destroy_multiple 
    Ticket.destroy(params[:tickets]) 
    respond_to do |format| 
     format.html { redirect_to tickets_path } 
     format.json { head :no_content } 
    end 
end 
Các vấn đề liên quan