rails - url_for 在使用命名空间时表现不同(基于当前使用的控制器) [英] rails - url_for behaving differently when using namespace (based on current controller being used)
问题描述
假设命名空间是abc",我们有一个控制器abcs",另一个使用命名空间abc"的控制器是defs".
lets assume namespace is "abc", we have a controller "abcs" and another one that uses namespace "abc" is "defs".
为了便于理解:
AbcsController
Abc::DefsController
当当前流在 AbcsController
中时,url_for({:controller => "abcs", :action => :new})
返回正确的 url 但当流在 Abc::DefsController
中时,当我给出:
When current flow is in AbcsController
, url_for({:controller => "abcs", :action => :new})
is returning correct url but when flow is in Abc::DefsController
, when I am giving:
url_for({:controller => "abcs", :action => :new })
它把它当作:
url_for({:controller => "abc/abcs", :action => :new}) #Observe abc/abcs
所以,这里应该是abcs"而不是abc/abcs",但它是这样对待的.
So, here it should be "abcs" but not "abc/abcs" but it is treating like that.
解决办法是什么?请向我询问更多信息.
Whats the solution? Please ask me fr more information.
推荐答案
来自这里:http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing
最后一个注释框是:
如果您需要在命名空间内使用不同的控制器命名空间块您可以指定绝对控制器路径,例如:get '/foo' =>'/foo#index'.
If you need to use a different controller namespace inside a namespace block you can specify an absolute controller path, e.g: get '/foo' => '/foo#index'.
这意味着您所要做的就是:
This means all you have to do is this:
url_for({ controller: "/abcs", action: :new })
我也遇到了这个问题,这解决了它:)
I ran into this problem as well and this solves it :)
这篇关于rails - url_for 在使用命名空间时表现不同(基于当前使用的控制器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!