3
我在sinatra重建一個小軌道(太矯枉過正)的應用程序。我有這樣的路線:西納特拉路線正則表達式約束?
match 'verify/:name/:bundle/:license' => 'verify#index', :constraints => { :bundle => /.*/ }
我怎麼能rebould它西納特拉在約束屬性中的項?
謝謝!
我在sinatra重建一個小軌道(太矯枉過正)的應用程序。我有這樣的路線:西納特拉路線正則表達式約束?
match 'verify/:name/:bundle/:license' => 'verify#index', :constraints => { :bundle => /.*/ }
我怎麼能rebould它西納特拉在約束屬性中的項?
謝謝!
您可以這樣來做:(從Sinatra's documentation拍攝)
get %r{/hello/([\w]+)} do
"Hello, #{params[:captures].first}!"
end
或者塊本身內:
get '/hello/:name' do
raise Sinatra::NotFound unless params[:name].match /\w+/
"Hello, #{params[:name]}!"
end
如果你使用Ruby 1.9,你可以使用一個名爲捕獲在正則表達式路由中,像這樣:
require 'sinatra'
get %r{verify/(?<name>\w+)/(?<bundle>.*)/(?<license>\w+)} do |name, bundle, license|
# do stuff
end