双命名空间路由问题
我正在尝试遵循这个 示例。 我在控制器中创建了一个操作:
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 paths
。我确实看到了这一点:
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}
I'm trying to follow this example.
I've created an action in my controller:
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
and I created a route in my `config/routes.rb' file:
namespace :admin do
namespace :distributions do
resources :workflows do
collection do
post :edit_multiple
put :update_multiple
post :distribute_resume
end
end
end
end
I also tried moving the route to the action outside of the collection block like this:
namespace :admin do
namespace :distributions do
resources :workflows do
post :distribute_resume
collection do
post :edit_multiple
put :update_multiple
end
end
end
end
But i'm getting this error in both cases:
No route matches {:controller=>"admin/distributions/workflows_controller", :distribution_id=>123, :action=>"distribute_resume", :method=>:post}
I'm too green to figure this out.
update:
ah yes, need to remember to check rake routes
more often. I do see this:
admin_distributions_workflow_distribute_resume POST /admin/distributions/workflows/:workflow_id/distribute_resume(.:format) {:action=>"distribute_resume", :controller=>"admin/distributions/workflows"}
so I changed my view:
<%=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%>
but i'm still getting a similar error message:
No route matches {:controller=>"admin/distributions/workflows", :distribution_id=>121, :action=>"distribute_resume", :method=>:post}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两个问题:
第一
您在 POST 请求期间没有传入
:workflow_id
。如果你查看你的rake paths
结果,你会发现这是必要的:第二
当你像这样命名路由时,你就告诉它你也反映了这一点控制器内的命名空间也是如此。
因此
,这意味着您需要在控制器中执行此操作:
如果您不想像那样为控制器命名空间,那么您需要将路由语法切换为:
这仍然会为您提供相同的路由方案,但不会强制您像以前一样执行控制器模块前缀。请记住,如果您切换到作用域方法,您的路径名将会更改,因此运行
rake paths
来获取新的路径名。更多信息:http://guides.rubyonrails.org/routing.html# controller-namespaces-and-routing
更新:
我认为你让这个变得比它需要的更复杂一些。您的
link_to
可以简化为:Two issues:
First
You are not passing in
:workflow_id
during your POST request. If you look at yourrake routes
results, you'll see it is necessary:Second
When you namespace the routes like that, you are telling it that you have also reflected that namespacing within the Controller as well.
So
Means that you would need to do this in your controller:
If you'd rather not namespace your controllers like that, then you need to switch up your routing syntax to instead be:
which will still give you the same routing scheme but will not force you to do the controller module prefixes like before. Keep in mind if you switch to the scoped method, your path names will change so run
rake routes
to get the new ones.More info: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
Update:
I think you're making this a little more complicated then it needs to be. Your
link_to
can be simplified to this:您拥有作为成员的
distribute_resume
操作,而不是集合操作。这是你的意图吗?您将其称为收集操作。因此,要么将路由声明移至
collection do
部分(如果它应该是一个集合操作),要么在重定向中传入工作流 ID。无论哪种方式,您都还必须重命名重定向路径,因为它实际上并不调用
distribute_resume
操作,而是调用索引操作。您当前拥有:
并且需要将其重命名为(集合版本):
或(成员版本):
You've got your
distribute_resume
action as a member, not a collection, action. Is this what you intended? You're calling it as if it was a collection action.So, either move your route declaration into the
collection do
portion (if it's supposed to be a collection action), or pass in a workflow ID in your redirect.Either way you're also going to have to rename your redirect path, because it doesn't actually call the
distribute_resume
action, it calls the index action.You currently have:
And this will need to be renamed to something along the lines of either (collection version):
or (member version):