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

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 に答える