railsで複数のデータを一括削除(destory)するのはcheckboxで実装するのがいいと思う
結論
このような感じの実装になった
articles_controller.rb
def destroy_many respond_to do |format| if params[:deletes].blank? format.html { redirect_to admin_articles_path } format.json { render :json, status: :unprocessable_entity } # 多分間違っている TODO end delete_list = params[:deletes].keys ActiveRecord::Base.transaction do if Article.destroy(delete_list) format.html { redirect_to admin_articles_path, notice: 'Article was successfully destroyed.' } format.json { head :no_content } end end rescue end end
routes.rb
delete :articles, to: 'articles#destroy_many'
index.html.erb
<%= form_with(url: admin_articles_path, method: :delete, local: true) do |form| %> <table class="table is-fullwidth is-hoverable"> <thead> <tr> <th class="w5p"></th> <th class="w5p">ID</th> <th>記事タイトル</th> <th class="w20p" colspan="2"></th> </tr> </thead> <tbody> <% @articles.each do |article| %> <tr> <td><%= check_box_tag "deletes[#{ article.id }]", article.id %></td> <td>#<%= article.id %></td> <td> <%= link_to article.title, edit_admin_article_path(article) %> </td> <td> <%= link_to '詳細', admin_article_path(article), class: 'button' %> <%= link_to '削除', admin_article_path(article), method: :delete, data: { confirm: 'Are you sure?' }, class: 'button' %> </td> </tr> <% end %> </tbody> </table> <%= form.submit '一括削除', class: 'button' %> <% end %>