【问题标题】:Matching URLs with a trailing slash in Rails' routes.rb在 Rails 的 routes.rb 中匹配带有斜杠的 URL
【发布时间】:2013-02-04 21:50:00
【问题描述】:
【问题讨论】:
标签:
ruby-on-rails
url-routing
【解决方案1】:
我找到的一个解决方案是使用 request.env["REQUEST_URI"],其中包含随请求提交的原始 URL。不幸的是,由于它不是请求的直接字符串属性,因此需要custom matching object:
class TrailingSlashMatcher
def matches?(request)
uri = request.env["REQUEST_URI"]
!!uri && uri.end_with?("/")
end
end
AppName::Application.routes.draw do
match '/example/*path', constraints: TrailingSlashMatcher.new, to: redirect("/somewhere/")
end
这似乎有点过头了,所以希望有人有更优雅的方法。