我正在尝试效仿this的例子。我在我的控制器中创建了一个动作:
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‘文件中创建了一条路由:
namespace :admin do
namespace :distributions do
resources :workflows do
collection do
post :edit_multiple
put :update_multiple
post :distribute_resume
end
end
end
end我还尝试将路由移动到集合块之外的操作,如下所示:
namespace :admin do
namespace :distributions do
resources :workflows do
post :distribute_resume
collection do
post :edit_multiple
put :update_multiple
end
end
end
end 但我在这两种情况下都得到了这个错误:
No route matches {:controller=>"admin/distributions/workflows_controller", :distribution_id=>123, :action=>"distribute_resume", :method=>:post}我太没经验了,弄不明白这件事。
更新:
啊,是的,需要记得多检查一下rake routes。我确实看到了这一点:
admin_distributions_workflow_distribute_resume POST /admin/distributions/workflows/:workflow_id/distribute_resume(.:format) {:action=>"distribute_resume", :controller=>"admin/distributions/workflows"}所以我改变了我的观点:
<%=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%>但我仍然收到类似的错误消息:
No route matches {:controller=>"admin/distributions/workflows", :distribution_id=>121, :action=>"distribute_resume", :method=>:post}发布于 2011-12-22 00:34:43
两个问题:
First
在POST请求过程中,您没有传入:workflow_id。如果你看一下你的rake routes结果,你会发现这是必要的:
/admin/distributions/workflows/:workflow_id/distribute_resume(.:format)第二个
当您像这样命名路由时,您是在告诉它,您也已经在控制器中反映了该命名空间。
所以
namespace :admin do
namespace :distributions do
resources :workflows do
end
end
end这意味着您需要在控制器中执行此操作:
class Admin::Distributions::WorkflowsController < ApplicationController
# controller code goes here
end如果你不想这样命名你的控制器,那么你需要将你的路由语法改为:
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可以简化为:
<% =link_to "Send this resume to #{distribution.matching_profile.partner.email}",
admin_distributions_workflow_distribute_resume_path(distribution.id),
:remote => true,
:method => :post %>发布于 2011-12-22 00:39:26
您的distribute_resume操作是作为成员进行的,而不是集合操作。这是你想要的吗?您正在调用它,就好像它是一个收集操作一样。
因此,要么将您的路由声明移动到collection do部分(如果它被认为是一个收集操作),要么在您的重定向中传递一个工作流ID。
无论哪种方式,您都必须重命名您的重定向路径,因为它实际上并不调用distribute_resume操作,而是调用索引操作。
您目前拥有:
redirect_to admin_distributions_workflows_path并且需要将其重命名为类似于(集合版本)的名称:
redirect_to admin_distributions_workflows_distribute_resume_path或(成员版本):
redirect_to admin_distributions_workflows_distribute_resume_path(@some_workflow_or_distribution_object)https://stackoverflow.com/questions/8592828
复制相似问题