允许 Rails 路由参数乱序
Allow Rails route params to be disordered
我有以下路线:
get "/marflar/:id(/colour/:colour)(/wheels/:wheels), to: "marflar#index"
它匹配以下网址:
/marflar/123
/marflar/123/colour/red
/marflar/123/wheels/4
/marflar/123/colour/red/wheels/4
但这迫使我在轮子之前加上颜色,即以下内容不起作用:
/marflar/123/wheels/4/colour/red
如何规定匹配约束的顺序无关紧要?
希望,以下内容可以做到:
get "/marflar/:id(/:name_1/:colour)(/:name_2/:wheels)", constraints: { name_1: /color| wheels/, name_2: /color|wheels/ }, 到: "marflar#index"
请检查并告诉我。
我建议使用 url 比如 /marflar/:id?color=red&wheels=10.
想法是将 url 的一部分捕获到参数中,然后以某种方式解析它们。
根据 Rails Routing,您可以使用 路由通配符和通配符段
从指南中摘录,例如:
get 'photos/*other', to: 'photos#unknown'
此路由将匹配 photos/12
或 /photos/long/path/to/12
,将 params[:other]
设置为 12
或 long/path/to/12
。以星号为前缀的片段称为 "wildcard segments".
你的情况:
get "/marflar/:id/*options", to: "marflar#index"
与之前的回答一样,这不会单独捕获参数,但您可以选择下一步要做什么。
我有以下路线:
get "/marflar/:id(/colour/:colour)(/wheels/:wheels), to: "marflar#index"
它匹配以下网址:
/marflar/123
/marflar/123/colour/red
/marflar/123/wheels/4
/marflar/123/colour/red/wheels/4
但这迫使我在轮子之前加上颜色,即以下内容不起作用:
/marflar/123/wheels/4/colour/red
如何规定匹配约束的顺序无关紧要?
希望,以下内容可以做到:
get "/marflar/:id(/:name_1/:colour)(/:name_2/:wheels)", constraints: { name_1: /color| wheels/, name_2: /color|wheels/ }, 到: "marflar#index"
请检查并告诉我。
我建议使用 url 比如 /marflar/:id?color=red&wheels=10.
想法是将 url 的一部分捕获到参数中,然后以某种方式解析它们。
根据 Rails Routing,您可以使用 路由通配符和通配符段
从指南中摘录,例如:
get 'photos/*other', to: 'photos#unknown'
此路由将匹配 photos/12
或 /photos/long/path/to/12
,将 params[:other]
设置为 12
或 long/path/to/12
。以星号为前缀的片段称为 "wildcard segments".
你的情况:
get "/marflar/:id/*options", to: "marflar#index"
与之前的回答一样,这不会单独捕获参数,但您可以选择下一步要做什么。