微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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)

原文地址:https://www.jb51.cc/ruby/269208.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐