如何在Sinatra中创建不区分大小写的路由?

时间:2010-12-23 18:33:14

标签: sinatra

我正在玩Sinatra,我想让我的一条路线不区分大小写。我尝试添加这样的路线:

get "(?i)/tileflood/?" do
end

但它与预期的/ tileflood的任何排列都不匹配。我在rubular.com上测试了以下正则表达式,它匹配得很好。我错过了什么吗?

\/(?i)tileflood\/?

1 个答案:

答案 0 :(得分:8)

你想要一条真正的正则表达式:

require 'sinatra'
get %r{^/tileflood/?$}i do
  request.url + "\n"
end

证明:

smagic:~ phrogz$ curl http://localhost:4567/tileflood
http://localhost:4567/tileflood

smagic:~ phrogz$ curl http://localhost:4567/tIlEflOOd
http://localhost:4567/tIlEflOOd

smagic:~ phrogz$ curl http://localhost:4567/TILEFLOOD/
http://localhost:4567/TILEFLOOD/

smagic:~ phrogz$ curl http://localhost:4567/TILEFLOOD/z
<!DOCTYPE html>
<html>
<head>
  <style type="text/css">
  body { text-align:center;font-family:helvetica,arial;font-size:22px;
    color:#888;margin:20px}
  #c {margin:0 auto;width:500px;text-align:left}
  </style>
</head>
<body>
  <h2>Sinatra doesn't know this ditty.</h2>
  <img src='/__sinatra__/404.png'>
  <div id="c">
    Try this:
    <pre>get '/TILEFLOOD/z' do
  "Hello World"
end</pre>
  </div>
</body>
</html>

smagic:~ phrogz$ curl http://localhost:4567/tileflaad
<!DOCTYPE html>
<html>
<head>
  <style type="text/css">
  body { text-align:center;font-family:helvetica,arial;font-size:22px;
    color:#888;margin:20px}
  #c {margin:0 auto;width:500px;text-align:left}
  </style>
</head>
<body>
  <h2>Sinatra doesn't know this ditty.</h2>
  <img src='/__sinatra__/404.png'>
  <div id="c">
    Try this:
    <pre>get '/tileflaad' do
  "Hello World"
end</pre>
  </div>
</body>
</html>