7

我有使用复选框进行多次删除的问题。当我删除多条记录时,它会获取复选框的 id,但它会将方法名称作为参数传递并显示错误。

这是我的代码,

  **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"}
4

4 回答 4

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

步骤: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
于 2014-01-27T06:05:53.330 回答
3

Ticket.destroy(array_of_ids)
于 2013-06-18T12:18:08.190 回答
3

试试这个

Ticket.where(:id => params[:ticket_ids]).destroy_all
于 2013-06-18T12:18:42.783 回答
2

嗨,同样更新您的控制器代码..

def destroy_multiple
 @tickets = Ticket.find(params[:ticket_ids])
 @tickets.each do |ticket|
 ticket.destroy
 end
end
于 2013-06-18T12:06:00.007 回答