【问题标题】:Difference between a wildcard and a named route in SinatraSinatra 中通配符和命名路由的区别
【发布时间】:2017-03-31 01:16:16
【问题描述】:

我一直在看几个关于 Sinatra 的教程,但找不到区别,比如说:

get "/hey/*/there/*"

对比

get "/hey/:first/there/:second"

我看到的唯一区别是访问参数的方式。因此,在第一种方式中,您可以通过以下方式访问它:

params[:splat][0]
params[:splat[1]

而第二种方式:

params[:first]
params[:second]

是 Sinatra 中的命名参数,只是我们为其命名的通配符吗?这是唯一的区别吗?

【问题讨论】:

    标签: ruby sinatra rack


    【解决方案1】:

    它们并不完全相同。如果您想匹配不确定数量的参数,而不是单个(命名)参数,则 Splats 会很有用。例如:

    # Will match: "/hey/:first/there/:second"
    # Will match: "/hey/*/there/*"
    GET /hey/first/there/second
    
    # Won't match: "/hey/:first/there/:second"
    # Will match: "/hey/*/there/*"
    GET /hey/first/second/there/third/fourth
    

    【讨论】:

      猜你喜欢
      • 2015-07-29
      • 2015-10-26
      • 2017-09-09
      • 1970-01-01
      • 2023-01-19
      • 2019-02-19
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      相关资源
      最近更新 更多