Ruby on Rails routes are essential to a Rails application’s function. The Rails routing system checks the URLs of incoming requests and decides what action or actions the application should take in response. The Rails router accomplishes this task by following the rules that the developer specifies in the configuration file config/routes.rb.
Besides mapping URL requests to application actions, the Ruby on Rails routing system also writes URLs that developers can use as arguments to methods such as redirect_to, link_to, and form_tag. The Rails router is a useful, two-way routing tool capable of more than simple controller/action sequences.
The Ruby on Rails configuration file, config/routes.rb, has default rules already in place that act in a pretty impressive manner. However, if you truly want to take advantage of the flexibility of the Rails router, you will need to write custom rules using the back-end language Ruby.
It is important to note right now, if you’re building a web application using Ruby on Rails, your configuration file, config/routes.rb, controls every URL aspect of your application. Let’s get an overview of Ruby on Rails routes. We’ll spend some time explaining broad routing terms and concepts that could aid you in your app development project.
Creating a Ruby on Rails Route
When creating a route on Ruby on Rails, you are assigning a controller and an action to a URL. URLs are mapped to a controller’s action using HTTP verbs:
For example, if you had a URL that looked like this /sunglasses/raybans and you wanted users who clicked on your link to be able to view your inventory of Rayban sunglasses, you would create a Ruby on Rails route that looked something like this:
get ‘sunglasses/:id’ to: ‘sunglasses#show’
This is a basic route where the controller is sunglasses, and the action is show. This basic pattern, controller#action, will be used to call the controller/action methods in your Rails router.
You can customize controller actions in Ruby on Rails, but remember that Rails prioritizes convention over configuration. Following conventions speeds up the development process, and in most cases, all actions you will require are standardized in Rails.
In some cases, it may be beneficial for you to name your Rails routes. After all, app development projects can get very large. By naming your routes, you can easily keep track of them all and call them where needed. Naming Rails routes is simple. All you need to do is add a parameter using :as.
For example, if your app requires user authentication, you’ll want to name your login and logout routes to keep them separated and labeled. Logins require the :new action, and logouts require the :destroy action. You wouldn’t want to mix these two controller actions on your app. This would be bad for UX.
Here is a quick example of how named routes look in Ruby on Rails:
get ‘login’ to: ‘sessions:new’, as: ‘login’
get ‘logout’ to: ‘sessions:destroy’, as: ‘logout’
Using names allows you to quickly call the path for the controller action that you require.
Sometimes naming Rails routes is not enough in terms of organization. In the event that you want to further organize your routes, you can group closely related routes together using the scope method.
Using the scope method of organizing Rails routes, you can set default options for all of the routes in the group. This comes in handy when you have something like content. Generally, you’d have the ability to create new content, edit existing content, and draft content. Each of these specific actions routes to the same controller.
The scope method allows you to reduce the amount of code you need to write by setting default options for all actions under the scope instead of writing out each action, as we have seen in previous examples. You can also use :as parameters to label scopes too. It is easy to imagine having different scopes for blogs, articles, and service pages.
Scope routes give Ruby on Rails developers the ability to code faster by reducing the amount of necessary code and stay organized by grouping related routes together. Keeping your Rails routes organized is crucial for ongoing development and regular maintenance.
Nested routes are similar to scope routes, but instead of lumping routes together in a single group, nested routes place baby routes under a single parent route. For example, using the sunglasses example from above, you could nest various brands under the sunglasses resource. It would look something like this:
resources :sunglasses do
You want to avoid over-nesting your resources. If nested routes go too deep, the URL paths become cumbersome. It is okay to nest brands under the sunglasses resource and even maybe nest sunglasses under eyewear, but you wouldn’t want to nest your routes much further than three levels.
Of all the routes in your application, the root route is the most important. The root route of your app or website is called upon when a request is made to your domain name. For example, if a user were to enter the URL of your website, the root route is what would be executed when they visit your page.
Your root route could simply call the homepage, or you could create an action controller rule that shows visitors a welcome message or a particular part of your homepage content. The root route is the main action controller of your Ruby on Rails application.
Be sure that you are choosing an appropriate root route to create the best possible UX. For example, new users don’t want to immediately land on a contact page or a blog post.
When creating a Ruby on Rails application, there are going to be a lot of routes before your development project is completed. Is there an easy way to view all Rails routes currently present in the application? The answer is yes, but you need to use the rake task that comes standard in all Rails applications.
When you use your terminal to run a rake routes command in Ruby on Rails, you will see a complete list of all of your Rails routes. This list will include:
- The name of the route
- The HTTP verb it uses
- The URL pattern of the route
- The controller action parameters
Rake routes are valuable to developers because it allows them to see all of their Rails routes in one place. This can help them determine what routes, if any, are still needed in their project and check to make sure that all routes are set up correctly.
Rake routes can also be used to call up the routes of specific controllers too. This allows Rails developers to quickly pinpoint specific routes and their associated scopes.
The use cases for Ruby on Rails are very diverse. This development framework has been used on a number of successful apps and websites, and every single one of them has had to manage their Rails routes. This overview is a simple breakdown of the main concepts in Rails routing.
Creating and managing an entire application’s worth of routes can be a very complicated, tiring task. If you need help or direction, reach out to an app development partner. The industry experience and technical expertise that a development partner can bring to the table is an invaluable resource when developing a Ruby on Rails application.