使用Sinatra,我可以通过使用以下命令将多个“未知”参数传递给一个路由:
get '/say/*/to/*' do
# matches /say/hello/to/world
params[:splat] # => ["hello", "world"]
end如何在Espresso中做同样的事情?
发布于 2012-12-05 05:44:02
Espresso中的路由是常规的Ruby方法。
因此,如果该方法在Ruby中有效,那么路由也将在Espresso中有效。
Ruby免费提供了您想要实现的功能。
只需定义一个带有预定义参数的Ruby方法:
require 'e'
class App < E
map '/'
def say greeting = :hello, vertor = :to, subject = :world
"say #{greeting} #{vertor} #{subject}"
end
end
# some testing
require 'sonar' # same as rack-test but a bit better
include Sonar
app App # letting Sonar know that app to test
puts get('/say').body
# => say hello to world
puts get('/say/Hi').body
# => say Hi to world
puts get('/say/Hi/from').body
# => say Hi from world
puts get('/say/Hello/from/Espresso').body
# => say Hello from Espresso
puts get('/say/Goodbye/to/Sinatra').body
# => say Goodbye to SinatraWorking Demo
https://stackoverflow.com/questions/13711811
复制相似问题