vendredi 13 novembre 2020

Rails/Ajax checbox delete completed is not working

I am trying to do a bulk delete using check boxes for list items. The items belong to lists which belong to projects. I think that I have everything wired up right but I am getting this error which leads me to believe that a before_action is causing the issue.

enter image description here

_item.html.erb

<li class="list-group-item" id="<%= dom_id(item) %>">
  <div class="checkbox">
    <label>
      <input type="checkbox" name="collection_ids[]" value="<%= item.id %>"  >
      <%= item.content %>
    </label>
  </div>
  <div>
    <span class="glyphicon glyphicon-resize-vertical handle"></span>
    <%= link_to %(<span class="glyphicon glyphicon-pencil"></span>).html_safe, [:edit, item], remote: true %>
    <%= link_to %(<span class="glyphicon glyphicon-trash"></span>).html_safe, item, remote: true, method: :delete, data: { confirm: "Are you sure?" } %>
  </div>
</li>

_list.html.erb

<%= form_for :items, url: "/items/bulk_destroy", method: :delete do |f| %> 
  <ul class="list-group sortable" data-update-url="<%= sort_list_path(list) %>">
    <%= render list.items %>
  </ul>
  <%= f.submit %>
<% end %> ```
'routes.rb`
```Rails.application.routes.draw do
  resources :projects do
    member do
      post :sort
    end
    resources :lists, shallow: true do
      member do
        post :sort
      end
      resources :items, shallow: true do 
        collection do 
          delete :bulk_destroy
        end
      end
    end
  end

  namespace :api do
    resources :scores, only: [:index, :create]
  end

  root 'projects#index'
end

items controller

class ItemsController < ApplicationController
  before_action :set_item, only: %i[edit update destroy], except: %i[bulk_destroy]

  def create
    @list = List.find(params[:list_id])
    @item = @list.items.create params[:item].permit(:content)
  end

  def edit;
    @item = Item.find(params[:id])
  end

  def update
    @item.update_attributes(params[:item].permit(:content))
  end

  def destroy
    @item.destroy
  end

  def bulk_destroy
    Item.where(id: params[:collection_id]).destroy_all
  end

  private

  def set_item
    @item = Item.where(id: params[:id]).first!
  end
end



Aucun commentaire:

Enregistrer un commentaire