Jaeger
Jaeger

Reputation: 1754

How can I use wildcards in my routes for all of them excepted for one pattern?

I would like to use React-Router to handle the majority of my pages from the visitor part and create a Single Page Application in the mean time. However, I would like to make sure that if the user types /admin, he wouldn't get "redirected" to the root_path

So far I only managed to either pass everything to a wildcard or make "compartiments" as follows:

root 'home#index'
# example 1
get '/*path' => 'home#index'
get '/articles' => 'home#articles_index'
# example 2
get '/articles/*all' => 'home#articles_index'
# vain try 1
get '/*path' => 'home#index', except: :admin
# vain try 2
get '/*path' => 'home#index', except: '/admin'

I found out about the constraints, but I realized it's to make sure the url is correctly passed (ie, integer instead of strings) but it doesn't "blacklist" urls.

Any idea?

Thank you in advance

Upvotes: 2

Views: 472

Answers (1)

Maksim Kalmykov
Maksim Kalmykov

Reputation: 1317

You can use wildcard segment with constraints for this purpose. For example:

# config/routes.rb
Rails.application.routes.draw do
  root to: 'home#index'
  get '*subroute', to: 'home#index', constraints: { subroute: /(?!admin|login|logout).*/ }

  # all another routes below
end

This will pass any request to home#index unless there is 'admin' or 'login' or 'logout' in URL.

Upvotes: 2

Related Questions