In Rails, we know that the routes are the most important entry-point where we define the which url hits which controller. We also know how to define the resources and add some conditions to the routing.
In this post, I want to concentrate about how to go about complex route conditions and make it easy for you to write modular code.
Let's say that you have to write a condition that if the domain belongs to a given domain, route the root url to some other controller. You can accomplish it using the following snippets:
Here you are creating a class for the domain constraint and having the required method called matches? which does the computing on the fly.
class DomainConstraint
def initialize(domain)
@domains = [domain].flatten
end
def matches?(request)
@domains.include? request.domain
end
end
Now you add the code to the routes.rb as following:
constraints DomainConstraint.new('mydomain.com') do
root :to => 'mydomain#index'
end
The above code makes sure that the requests coming to a mydomain.com will have a root url at the mydomain controller and index action!
Happy Railing!



![[VB.NET] Finding whether its Design Mode or Runtime Mode for Forms](https://www.jeygeethan.com/rails/active_storage/representations/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBjUT09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--83e9b1eefb2dbf28cf8ba95e611a9763be3b888b/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCem9MWm05eWJXRjBTU0lKYW5CbFp3WTZCa1ZVT2hKeVpYTnBlbVZmZEc5ZlptbDBXd2RwQWlBRGFRSWdBdz09IiwiZXhwIjpudWxsLCJwdXIiOiJ2YXJpYXRpb24ifX0=--6537b47e9694a3a864616c0c841b2b90918ae094/DesignTime.jpeg)