2013-02-04 2 views
1

요청 URL에 슬래시가 있는지 여부에 따라 Rails의 routes.rb에서 다른 라우팅을 수행 할 수 있습니까? 요청 객체의 후행 슬래시가 제거되었으므로 GET에 http://www.example.com/about/의 request.url 값은 http://www.example.com/about이므로이 작업은 어려워 보입니다. 이 동작은 request-based constraintsroute-globbing을 사용한 일치를 방지합니다.Rails의 routes.rb에 후행 슬래시가있는 URL 일치하기

답변

1

내가 찾은 한 가지 해결책은 요청과 함께 제출 된 원시 URL을 포함하는 request.env [ "REQUEST_URI"]를 사용하는 것입니다. 가 요청의 직접적인 문자열 속성이 아닙니다 이후 불행하게도, 그것은 필요로하는 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 

.

관련 문제