Rails 3通过复选框破坏多条记录
问题描述:
我有多个使用复选框删除的问题。当我删除多个记录时,它获得了复选框的ID,但它传递一个方法名称作为参数,并显示我错误。Rails 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]
通行证arguement ::::
{"utf8"=>"✓",
"_method"=>"delete",
"authenticity_token"=>"yHeRR49ApB/xGq1jzMTdzvix/TJt6Ysz88nuBEotHec=",
"ticket_ids"=>["11",
"12"],
"commit"=>"Delete selected",
"id"=>"destroy_multiple"}
答
做
Ticket.destroy(array_of_ids)
答
嗨,更新您的控制器代码同样..
def destroy_multiple
@tickets = Ticket.find(params[:ticket_ids])
@tickets.each do |ticket|
ticket.destroy
end
end
答
尝试此
Ticket.where(:id => params[:ticket_ids]).destroy_all
答
步骤: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
其中u使用 “@ticketsts” 这个???这是更正和完善的代码....“Ticket.destroy(params [:ticket_ids])” – SSR 2013-06-21 05:58:56
它的错字,我已经纠正它@SSR – Radhakrishna 2013-09-25 11:03:43