Redirecting from routes in case of restrictions breakdown

I want to redirect to another url when route restriction fails

Route.rb

match '/ u' => 'user # signin' ,: constraints => BlacklistDomain

blacklist_domain.rb

class BlacklistDomain BANNED_DOMAINS = ['domain1.com', 'domain2.com'] def matches?(request) if BANNED_DOMAINS.include?(request.host) ## I WANT TO REDIRECT HERE WHEN THE CONDITION FAILS else return true end end end 
+6
source share
1 answer

Because Rails routes run sequentially, you can simulate a conditional login as follows:

 # config/routes.rb match '/u' => 'controller#action', :constraints => BlacklistDomain.new match '/u' => 'user#signin' 

The first line checks if the restriction conditions are fulfilled (i.e. if the request comes from a blacklisted domain). If the restriction is met, the request is sent to controller#action (respectively, replace accordingly).

If the restriction conditions are not met (i.e., the request is not blacklisted), the request will be redirected to user#signing .

Since this conditional logic is processed efficiently in your routes, your restriction code can be simplified:

 # blacklist_domain.rb class BlacklistDomain BANNED_DOMAINS = ['domain1.com', 'domain2.com'] def matches?(request) if BANNED_DOMAINS.include?(request.host) return true end end end 
+7
source

Source: https://habr.com/ru/post/956875/


All Articles