Ruby on rails 任意斜线数的Rails路由
我有一个自定义路由,它根据输入的url检查重定向 例如/site可能重定向到/mysite 代码如下:Ruby on rails 任意斜线数的Rails路由,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我有一个自定义路由,它根据输入的url检查重定向 例如/site可能重定向到/mysite 代码如下: class RedirectCheck def initialize @redirects = Redirect.all_from_paths end def matches?(request) @redirects.include?(request[:path]) end end MyApp::Application.routes.draw do ge
class RedirectCheck
def initialize
@redirects = Redirect.all_from_paths
end
def matches?(request)
@redirects.include?(request[:path])
end
end
MyApp::Application.routes.draw do
get ":path" => 'redirects#show', constraints: RedirectCheck.new
end
Redirect.all_from_paths基本上是一个模型方法,它返回所有接受路由的数组,然后“redirects#show”执行实际的重定向
现在我的问题是路线
get ":path" => 'redirects#show', constraints: RedirectCheck.new
..将不接受带有斜线的路径
例如,我无法添加/go/some/这样的路径,因为我的重定向路由无法识别它
如何更改此行,使其接受任何斜杠数的路径,并将其作为参数[:path]传递到“redirects#show”?您需要的是:
get "*path", to: 'redirects#show', constraints: RedirectCheck.new
还有很多其他的例子。这很好,但是如果你能添加更多的细节,那就更好了-别忘了total noobs会出现在这个网站上,希望了解为什么这样做。也许可以为他们解释一下
*
的属性:)好吧,现在我觉得没有尝试一下很愚蠢:)