ruby-on-rails-3 – Rails 3通过复选框销毁多个记录

前端之家收集整理的这篇文章主要介绍了ruby-on-rails-3 – Rails 3通过复选框销毁多个记录前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有使用复选框多次删除的问题.当我删除多个记录时,它会获得复选框的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"}

解决方法

Ticket.destroy(array_of_ids)

猜你在找的Ruby相关文章