Rails 3 通过复选框销毁多条记录
问题描述:
我在使用复选框进行多次删除时遇到问题.当我删除多条记录时,它会获取复选框的 id,但它正在传递一个方法名称作为参数并显示错误.
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.
这是我的代码,
**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]
通过争论::::
{"utf8"=>"✓",
"_method"=>"delete",
"authenticity_token"=>"yHeRR49ApB/xGq1jzMTdzvix/TJt6Ysz88nuBEotHec=",
"ticket_ids"=>["11",
"12"],
"commit"=>"Delete selected",
"id"=>"destroy_multiple"}
答
do
Ticket.destroy(array_of_ids)