Rails supports concerns in Routes. Concerns can be defined in Rails routes to be able to have reusable routes.
Sometimes, we need similar routes for different resources. Let’s take an example.
Let’s say, we have a
User can have an image. Thus, we need image resource for user resource routes.
It can be defined in
as given below.
Post resources can have image.
It can be defined in routes as given below.
As we can see, the routes defined above, are not very DRY.
Use of concerns in Rails routes
We can use Routing concerns, to avoid repeating same routes defined.
Here, we have defined a concern
It is used with resource
As the key
it can be used to have multiple concerns for a resource.
Multiple concerns in routes
We can have multiple concerns, used for a resource or resources.
Let’s say, we have
concerns as given below.
The above code is similar to routes defined below.
posts will use both concerns
user will only have
image resource because of single concern used with it.
Subscribe to Ruby in Rails
Get the latest posts delivered right to your inbox