首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >双重命名空间路由问题

双重命名空间路由问题
EN

Stack Overflow用户
提问于 2011-12-22 00:09:59
回答 2查看 960关注 0票数 2

我正在尝试效仿this的例子。我在我的控制器中创建了一个动作:

代码语言:javascript
复制
  def distribute_resume
    Rails.logger.info(distribution_id.to_s)
    PartnerNotifier.distribute_resume(distribution_id)
    flash[:notice] = "Successfully distributed resume"
    redirect_to admin_distributions_workflows_path
  end

我在我的` `config/routes.rb‘文件中创建了一条路由:

代码语言:javascript
复制
  namespace :admin do
    namespace :distributions do
      resources :workflows do
        collection do
          post :edit_multiple
          put :update_multiple
          post :distribute_resume
        end
      end
    end
  end

我还尝试将路由移动到集合块之外的操作,如下所示:

代码语言:javascript
复制
  namespace :admin do
    namespace :distributions do
      resources :workflows do
        post :distribute_resume
        collection do
          post :edit_multiple
          put :update_multiple
        end
      end
    end
  end 

但我在这两种情况下都得到了这个错误:

代码语言:javascript
复制
No route matches {:controller=>"admin/distributions/workflows_controller", :distribution_id=>123, :action=>"distribute_resume", :method=>:post}

我太没经验了,弄不明白这件事。

更新:

啊,是的,需要记得多检查一下rake routes。我确实看到了这一点:

代码语言:javascript
复制
admin_distributions_workflow_distribute_resume POST   /admin/distributions/workflows/:workflow_id/distribute_resume(.:format) {:action=>"distribute_resume", :controller=>"admin/distributions/workflows"}

所以我改变了我的观点:

代码语言:javascript
复制
      <%=link_to "Send this resume to #{distribution.matching_profile.partner.email}",
            :controller => "workflows", <-- instead of "workflows_controller"
            :action => "distribute_resume",
            :distribution_id => distribution.id,
            :method => :post%>

但我仍然收到类似的错误消息:

代码语言:javascript
复制
No route matches {:controller=>"admin/distributions/workflows", :distribution_id=>121, :action=>"distribute_resume", :method=>:post}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-12-22 00:34:43

两个问题:

First

在POST请求过程中,您没有传入:workflow_id。如果你看一下你的rake routes结果,你会发现这是必要的:

代码语言:javascript
复制
/admin/distributions/workflows/:workflow_id/distribute_resume(.:format)

第二个

当您像这样命名路由时,您是在告诉它,您也已经在控制器中反映了该命名空间。

所以

代码语言:javascript
复制
namespace :admin do
  namespace :distributions do
    resources :workflows do
    end
  end
end

这意味着您需要在控制器中执行此操作:

代码语言:javascript
复制
class Admin::Distributions::WorkflowsController < ApplicationController
  # controller code goes here
end

如果你不想这样命名你的控制器,那么你需要将你的路由语法改为:

代码语言:javascript
复制
scope "/admin" do
  scope "/distributions" do
    resources :workflows do
    end
  end
end

它仍然会为您提供相同的路由方案,但不会强制您像以前那样添加控制器模块前缀。请记住,如果您切换到限定了作用域的方法,您的路径名将会更改,因此运行rake routes来获取新的路径名。

更多信息:http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

更新:

我认为你让事情变得比需要的复杂了一点。您的link_to可以简化为:

代码语言:javascript
复制
<% =link_to "Send this resume to #{distribution.matching_profile.partner.email}",
            admin_distributions_workflow_distribute_resume_path(distribution.id),
            :remote => true,
            :method => :post %>
票数 1
EN

Stack Overflow用户

发布于 2011-12-22 00:39:26

您的distribute_resume操作是作为成员进行的,而不是集合操作。这是你想要的吗?您正在调用它,就好像它是一个收集操作一样。

因此,要么将您的路由声明移动到collection do部分(如果它被认为是一个收集操作),要么在您的重定向中传递一个工作流ID。

无论哪种方式,您都必须重命名您的重定向路径,因为它实际上并不调用distribute_resume操作,而是调用索引操作。

您目前拥有:

代码语言:javascript
复制
redirect_to admin_distributions_workflows_path

并且需要将其重命名为类似于(集合版本)的名称:

代码语言:javascript
复制
redirect_to admin_distributions_workflows_distribute_resume_path

或(成员版本):

代码语言:javascript
复制
redirect_to admin_distributions_workflows_distribute_resume_path(@some_workflow_or_distribution_object)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8592828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档