首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Splat params with Espresso

Splat params with Espresso
EN

Stack Overflow用户
提问于 2012-12-05 04:59:20
回答 1查看 100关注 0票数 3

使用Sinatra,我可以通过使用以下命令将多个“未知”参数传递给一个路由:

代码语言:javascript
复制
get '/say/*/to/*' do
  # matches /say/hello/to/world
  params[:splat] # => ["hello", "world"]
end

如何在Espresso中做同样的事情?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-05 05:44:02

Espresso中的路由是常规的Ruby方法。

因此,如果该方法在Ruby中有效,那么路由也将在Espresso中有效。

Ruby免费提供了您想要实现的功能。

只需定义一个带有预定义参数的Ruby方法:

代码语言:javascript
复制
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 Sinatra

Working Demo

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13711811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档