Sinatra,尽管有请求方法,如何获得一切

时间:2013-08-19 09:02:28

标签: ruby sinatra

根据http://www.sinatrarb.com/intro.html#Request/Instance%20Scope

您有请求范围绑定:

  • get,head,post,put,delete,options,patch,link和unlink blocks
  • 过滤器之前和之后
  • 帮助方法
  • 模板/视图

是否有任何捷径可以完全相同,尽管有方法?

get // do
  # do something
end

post // do
  # do something exactly the same as previous
end

put // do
  # do something exactly the same as previous
end

delete // do
  # do something exactly the same as previous
end

...

1 个答案:

答案 0 :(得分:3)

不确定我是否理解你。但我认为你会同时捕获多个http动词,为此你可以使用multi route

示例

require 'sinatra'
require "sinatra/multi_route"
route :get, :post, '/foo' do
  # "GET" or "POST"
  p request.env["REQUEST_METHOD"]
end

# Or for module-style applications
class MyApp < Sinatra::Base
  register Sinatra::MultiRoute
  route :get, :post, '/foo' do
    # ...
  end
end