如何使用Rails通配符语法匹配索引和显示路由?

时间:2018-02-09 01:29:49

标签: ruby-on-rails routes glob

我想使用rails通配符路由来匹配特定命名空间中的任何内容。即我希望GET /foo/barget /foo重定向到/quz。我似乎无法弄清楚如何让它在routes.rb中工作:

# this only captures requests to `/foo/bar`, not `/foo`
namespace :foo do 
  match '*path', to :quz 
end

# this only captures requests to `/foo`, not `/foo/bar`
namespace :foo do 
  match '/', to :quz 
end

# I've resorted to this, which looks ugly. 
namespace :foo do 
  match '*path', to :quz 
  match '/', to :quz 
end

会喜欢任何建议!提前致谢。

1 个答案:

答案 0 :(得分:0)

*path设置为()的argmuents可选项。试试这个:

namespace :foo do 
  match '(*path)', to :quz, via: :get
end