Rails routes match full hostname with multiple per

2019-07-24 19:31发布

I'm trying to match a URL something like

http://example.com/this.is.my.full.hostname/something/else

apparently when I pass the param in the routes.rb file it doesn't recognize this parameter

my code says the following

match '/:computer_hostname/somethingelse' => 'test#info'

any ideas what's the right way to achieve the URL I wanted above ? Or is it even possible ? I know period is allowed character in URL but does it allow more than one ?

1条回答
Ridiculous、
2楼-- · 2019-07-24 19:55

I think the constraints method/option will help you out. Try something like the following:

match ':hostname/something/else' => 'test#info',
  :constraints => {:hostname => /[A-Za-z0-9\._\-]+/}

If you're doing multiple matches all with the same :hostname segment, then you can wrap them in a constraints method call:

constraints(:hostname => /[A-Za-z0-9\._\-]+/) do
  match ':hostname/something/else' => 'test#info'
  match ':hostname/foo/bar'        => 'test#foo'
end
查看更多
登录 后发表回答