2014-09-24 103 views
0

我有一個表格,其中每個用戶都有一個對應的複選框。點擊一個按鈕即可刪除所選的下列內容。ActionController :: UrlGenerationError No嵌套資源的路由匹配

下面是用戶下的嵌套資源,我在我的控制器中有一個銷燬方法,當我做耙路由時,我可以看到相應的路由#destroy操作,但是當下列列表頁面被加載時,它不會引發路由匹配錯誤。

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" 

耙路線:

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 

錯誤堆棧:

沒有路由匹配{:action=>"destroy", :controller=>"followings", :user_id=>"540f5c6b7072610a4c040000"} 提取的源(左右線#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 

謝謝!

+2

看起來像你的壓痕可能會關閉,如果代碼與您粘貼的代碼相同。應該是一個縮進,然後following.check_box? – Kevin 2014-09-24 20:05:06

+0

表單佈局看起來有點不尋常。 你能解釋爲什麼你在這個視圖中使用兩個不同的實例變量「@follow」和「@followings」?另外,爲什麼你在一個索引視圖中渲染一個表單(並不是說它是錯誤的,但通常不是索引的用途)。 – Kevin 2014-09-24 20:24:32

+0

@Kevin縮進在實際代碼中是正確的,只是在這裏粘貼了幾行而不是整個哈姆。 「@followings」是要在索引頁面上顯示的列表。 「@following」是用戶選擇刪除時的列表項目。 – Yeshasvi 2014-09-25 12:28:19

回答

0

路由問題已解決。對於多次刪除,路由必須聲明爲集合。這裏有其工作的變化 -

的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 
相關問題