Rails 3 Routing Constraint and Regex

后端 未结 2 1799
北海茫月
北海茫月 2021-01-07 10:23

I\'m looking to match the pattern state/city in the path, unless the state variable equals \"auth\"

match \'/:state/:city\' => \'cities#index         


        
2条回答
  •  无人及你
    2021-01-07 10:29

    You should be able to define your /auth route before your state/city routes:

    Route priority

    Not all routes are created equally. Routes have priority defined by the order of appearance of the routes in the config/routes.rb file. The priority goes from top to bottom.

    So this order should do the right thing:

    match '/auth/twitter' => ...
    match '/:state/:city' => ... 
    

    You might want to avoid the problem altogether by putting your state/city routes into their own namespace:

    match '/place/:state/:city' => ...
    

    That leaves the top level clear for other future uses.

提交回复
热议问题