Reputation: 311
I am trying to delete a collection of items based upon checkboxes. I created a custom route. The destroy_multiple route.
Rails.application.routes.draw do
resources :projects do
resources :lists, shallow: true do
member do
post :sort
end
resources :items, shallow: true
end
end
resources :items do
collection do
delete :destroy_multiple, :as => :destroy_multiple
end
end
resource :react
end
When I run rake routes I get this as a result
Prefix Verb URI Pattern Controller#Action
sort_list POST /lists/:id/sort(.:format) lists#sort
list_items GET /lists/:list_id/items(.:format) items#index
POST /lists/:list_id/items(.:format) items#create
new_list_item GET /lists/:list_id/items/new(.:format) items#new
edit_item GET /items/:id/edit(.:format) items#edit
item GET /items/:id(.:format) items#show
PATCH /items/:id(.:format) items#update
PUT /items/:id(.:format) items#update
DELETE /items/:id(.:format) items#destroy
project_lists GET /projects/:project_id/lists(.:format) lists#index
POST /projects/:project_id/lists(.:format) lists#create
new_project_list GET /projects/:project_id/lists/new(.:format) lists#new
edit_list GET /lists/:id/edit(.:format) lists#edit
list GET /lists/:id(.:format) lists#show
PATCH /lists/:id(.:format) lists#update
PUT /lists/:id(.:format) lists#update
DELETE /lists/:id(.:format) lists#destroy
projects GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project GET /projects/new(.:format) projects#new
edit_project GET /projects/:id/edit(.:format) projects#edit
project GET /projects/:id(.:format) projects#show
PATCH /projects/:id(.:format) projects#update
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy
destroy_multiple_items DELETE /items/destroy_multiple(.:format) items#destroy_multiple
items GET /items(.:format) items#index
POST /items(.:format) items#create
new_item GET /items/new(.:format) items#new
GET /items/:id/edit(.:format) items#edit
GET /items/:id(.:format) items#show
PATCH /items/:id(.:format) items#update
PUT /items/:id(.:format) items#update
DELETE /items/:id(.:format) items#destroy
react POST /react(.:format) reacts#create
new_react GET /react/new(.:format) reacts#new
edit_react GET /react/edit(.:format) reacts#edit
GET /react(.:format) reacts#show
PATCH /react(.:format) reacts#update
PUT /react(.:format) reacts#update
DELETE /react(.:format) reacts#destroy
And my form tag looks like this..
<%= form_tag url_for(:controller => 'items', :action => 'destroy_multiple'), :method => 'delete' do %>
<ul class="list-group sortable" data-update-url="<%= sort_list_path(list) %>">
<%= render list.items %>
</ul>
<%= submit_tag("Delete Items") %>
<% end %>
I am not getting any errors but it is not going to the right controller action it ends up at just the destroy action for a singular item. Here is my controller. class ItemsController < ApplicationController
before_action :set_item, only: [:edit, :update]
def create
@list = List.find(params[:list_id])
@item = @list.items.create params[:item].permit(:content)
end
def edit
end
def update
@item.update_attributes(params[:item].permit(:content))
end
def destroy
@item.destroy
end
def destroy_multiple
debugger
end
private
def set_item
@item = Item.where(id: params[:id]).first!
end
end
I just want the form submission to go to the custom destroy_multiple action. I appreciate any help. Thank You.
Upvotes: 0
Views: 254
Reputation: 7777
You can try to follow, at first you create route like original destroy
action
resources :items do
collection do
#delete :destroy_multiple, :as => :destroy_multiple
delete '/destroy_multiple/:id(.:format)' => 'items#destroy_multiple', constraints: { id: /.*/ }, as: :destroy_multiple
end
end
Now run rake routes
you get the something like this (not tested route but code is right now)
destroy_multiple_items DELETE /items/destroy_multiple/:id(.:format) items#destroy_multiple
Now you can try to delete using checkbox
def destroy_multiple
Item.where(id: params[:id]).destroy_all
# redirect
end
Hope to help
Upvotes: 0
Reputation: 311
As per @JoshHumphrey my error was having multiple routes. My updated route file is
Rails.application.routes.draw do
resources :projects do
resources :lists, shallow: true do
member do
post :sort
end
resources :items, shallow: true do
collection do
delete :destroy_multiple, :as => :destroy_multiple
end
end
end
end
resource :react
end
And I updated the form a bit to pass the list id down so here is the form
<%= form_tag(destroy_multiple_list_items_path(list_id: list.id),method: :delete,remote: true) do %>
<ul class="list-group sortable" data-update-url="<%= sort_list_path(list) %>">
<%= render list.items %>
</ul>
<%= submit_tag("Delete Selected", :class => "btn btn-warning") %>
<% end %>
Everything else stayed the same! Thanks for the help Josh.
Upvotes: 1