Rails路由以/:some_action重定向开头的任何路由

时间:2014-05-15 18:01:20

标签: ruby-on-rails rails-routing

是否可以将这些结合到一个陈述中?

  match '/:action', to: redirect('/'), constraints: {action: /properties/}, via: :all
  match '/:action/*other', to: redirect('/'), constraints: {action: /properties/}, via: :all

如果用户输入的根网址以/properties开头(例如www.google.com/propertieswww.google.com/properties/foo/barwww.google.com/properties/1/bar/foo/baz/test),我想重定向到根网址。所有这些示例都应该回到www.google.com

修改

我最终得到了match '/:action(/*anything)', to: redirect('/'), constraints: {action: /properties/}, via: :all

1 个答案:

答案 0 :(得分:2)

让它们成为可选项。

match '/:action(/:other)', to: redirect('/'), constraints: {action: /properties/, other: [a-zA-Z0-9\/\\]+ }, via: :all

来自doc

get ':controller(/:action(/:id))'
  

此路由还会将/ photos的传入请求路由到   PhotosController #index,因为:action和:id是可选参数,   用括号表示。