【问题标题】:ActionController::UrlGenerationError No Routes Matches for Nested ResourceActionController::UrlGenerationError 嵌套资源没有路由匹配
【发布时间】:2014-09-24 19:55:05
【问题描述】:

我有一个包含用户关注列表的表单,其中每个关注者都有一个相应的复选框。单击按钮将删除选定的以下内容。

followings 是用户下的嵌套资源,我的控制器中有一个 destroy 方法,当我执行 rake 路由时,我可以看到与 followings#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=&gt;"destroy", :controller=&gt;"followings", :user_id=&gt;"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

谢谢!

【问题讨论】:

  • 看起来你的缩进可能是关闭的,如果代码与你粘贴的相同。应该是缩进然后following.check_box?
  • 表单布局看起来有点不寻常。你能解释一下为什么你在这个视图中使用两个不同的实例变量“@following”和“@followings”吗?另外,为什么要在索引视图中渲染表单(不是说它是错误的,但通常不是索引的用途)。
  • @Kevin 缩进在实际代码中是正确的,只是在这里粘贴了几行而不是整个haml。 “@followings”是要在索引页面上显示的列表。 “@following”是用户选择要删除的列表项。

标签: ruby-on-rails ruby-on-rails-4


【解决方案1】:

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

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 

【讨论】:

    猜你喜欢
    • 2015-08-06
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    相关资源
    最近更新 更多