Software Development

Ruby on Rails - Complex route constraints made easy

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.
Like
by Jey Geethan | March 31, 2019
Ruby on Rails - Complex route constraints made easy

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!


Jey Geethan

Jey Geethan is a poet, author and an entrepreneur.


Related Articles

Why Microservices?
Software Development

Why Microservices?

by Jey Geethan | December 16, 2022
I have a few tidbits about why to use microservices and why it makes sense to create few microservices as a side project and learn from the same
Top 5 Tips for Software Development Managers To Conduct Effective 1 on 1s With Your Reports
Software Development

Top 5 Tips for Software Development Managers To Conduct Effective 1 on 1s With Your Reports

by Jey Geethan | October 05, 2020
Some of the effective tips that can enhance your 1 on 1s with your reports. In 2020, software development is one of the major industries which provides millions of jobs. In fact, if you look at the statistics, this industry is poised to grow even further. According to Slashdata, it's expected to reach 45 million developers by 2030. As a manager, it's your responsibility that 1 on 1s are effective and help your reports. I want to talk about a few of the things that make this process really effective.
Do you want to use your macbook as a lap-heater?
Software Development

Do you want to use your macbook as a lap-heater?

by Jey Geethan | August 28, 2019
How many times have you felt the coldness of a meeting room and wish you had brought a heater into the room. Now you can convert your macbook into a heater
How to get informative, color prompts on Mac Terminal or iTerm2
Software Development

How to get informative, color prompts on Mac Terminal or iTerm2

by Jey Geethan | August 14, 2019
This article talks about how to get color prompts on your terminal and add some informative things like current git branch and distinctive colors for current path, username etc.
Can you have a dinner conversation around Pull Requests?
Software Development

Can you have a dinner conversation around Pull Requests?

by Jey Geethan | July 11, 2019
I believe that writing code is a way of communication and it enables developers understand better than just talking about the concepts or logic in plain english words.