3

Sinatra を使用すると、次を使用して複数の「不明な」パラメーターをルートに渡すことができます。

get '/say/*/to/*' do
  # matches /say/hello/to/world
  params[:splat] # => ["hello", "world"]
end

エスプレッソで同じことをする方法は?

4

1 に答える 1

3

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 Sinatra

ワーキングデモ

于 2012-12-04T21:44:02.223 に答える