繁体   English   中英

ActionController :: UrlGenerationError没有路由匹配嵌套资源

[英]ActionController::UrlGenerationError No Routes Matches for Nested Resource

我有一个带有以下用户列表的表单,其中每个用户后面都有一个对应的复选框。 单击按钮将删除所选的跟随者。

followings是用户下的一个嵌套资源,我在控制器中有一个destroy方法,当我耙路径时,我可以看到与followings#destroy操作相对应的路由,但是在加载followings列表页面时,它不会引发任何路由匹配错误。

route.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

谢谢 !

路由问题解决了。 对于多次删除,必须将路由声明为集合。 这是有效的更改-

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 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM