I have a form with list of followings for a user where each following has a corresponding check box. The selected followings would be deleted on click of a button.
followings is a nested resource under users, i have a destroy method in my controller and when i do rake routes, i can see the route corresponding to followings#destroy action but when the followings list page is loaded it throws no routes matches error.
routes.rb:
resources :users do
resources :followings
resources :events
end
following_controller.rb:
class FollowingsController < ApplicationController
def index
@followings = Following.findFollowings(params.has_key?("user_id") ? params[:user_id] : current_user.id)
@following = Following.new
end
def destroy
Following.any_in(:following_id => params[:id]).destroy_all
render index
end
end
index.html.haml:
= form_for(@following, url: {action: 'destroy'}, :html => {:method => :delete, :role => 'form'}) do |f|
- @followings.each do |following|
= f.check_box "following_id"
= f.submit "Delete"
rake routes:
user_followings GET /users/:user_id/followings(.:format) followings#index
POST /users/:user_id/followings(.:format) followings#create
new_user_following GET /users/:user_id/followings/new(.:format) followings#new
edit_user_following GET /users/:user_id/followings/:id/edit(.:format) followings#edit
user_following GET /users/:user_id/followings/:id(.:format) followings#show
PATCH /users/:user_id/followings/:id(.:format) followings#update
PUT /users/:user_id/followings/:id(.:format) followings#update
DELETE /users/:user_id/followings/:id(.:format) followings#destroy
Error stack:
No route matches {:action=>"destroy", :controller=>"followings", :user_id=>"540f5c6b7072610a4c040000"}
Extracted source (around line #9):
6 %ul
7 = render partial: "shared/npo_menu", locals: {item: 'followings'}
8 %section.following.notifications
9 = form_for(@following, url: {action: 'destroy'}, :html => {:method => :delete, :role => 'form'}) do |f|
10 .container
11 .row.manipulate
12 .pull-left
Thanks !
The routing problem is solved. For multiple deletes the route had to declared as collection. here are the changes which worked -
routes.rb
resources :users do
resources :followings do
collection do
delete 'destroy_multiple'
end
end
resources :events
end
followings_controller.rb
def destroy_multiple
Following.any_in(:following_id => params[:id]).destroy_all
respond_to do |format|
format.html { redirect_to user_followings_path }
format.json { head :no_content }
end
index.haml.html
= form_tag destroy_multiple_user_followings_path, method: :delete do
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.