# Routing Concerns in Rails

*Published on August 07, 2024*

Tags: Router

---


This post was originally published in the&nbsp;[Rails Router handbook](/tag/router).

Sometimes, you have common routes that you want to reuse inside other resources and routes. For example, imagine that your application has two resources,&nbsp;`photos`&nbsp;and&nbsp;`posts`.

```ruby
# config/routes.rb

resources :posts
resources :photos
```

Next, you decide that you want to allow users to add comments under both posts and photos. That means you'll want to nest comments under both resources as follows:

```ruby
# config/routes.rb

resources :posts do
  resources :comments
end

resources :photos do
  resources :comments
end
```

This is simple enough. But, you can imagine this can get repetitive if you have few more '_commentable_' resources, i.e.&nbsp;**resources that can be commented.**

To avoid this repetition, Rails lets you to declare these common routes (**concerns**) to reuse inside other resources and routes. For this, you'll use the&nbsp;`concern`&nbsp;method:

```ruby
# config/routes.rb

# Define a concern
concern :commentable do
  resources :comments
end

# Use that concern
resources :posts, concerns: :commentable
resources :photos, concerns: :commentable
```

You are not limited to a single concern. A resource can use multiple concerns, too.

```ruby
resources :messages, concerns: [:commentable, :attachable, ...]
```

You can also use these concerns in other routing blocks by calling the&nbsp;`concerns`&nbsp;method.

```ruby
# config/routes.rb

namespace :blog do
  concerns :commentable
end
```

The underlying idea behind routing concerns is exactly similar to the concept of concerns in Rails:&nbsp;**to group commonly used, related code together in one place.**&nbsp;Check out the following post if you want to learn more about Rails concerns.

> [Concerns in Rails: Everything You Need to Know](/how-rails-concerns-work-and-how-to-use-them)

## How to Pass Options to a Concern

You can also pass options to a concern by using a block. This allows you to customize the behavior of the concern for different resources. For example, you might want to limit the actions available on a resource while still using the shared logic from the concern. Here's how you can do it:

```ruby
concern :commentable do |options|
  resources :comments, options
end
        
resources :images, concerns: :commentable

resources :published_posts do
  concerns :commentable, only: [:show]
end
```

In the example, the&nbsp;`commentable`&nbsp;concern is applied to the&nbsp;`published_posts`&nbsp;resource, but with a restriction:&nbsp;**only the**&nbsp;**`show`**&nbsp;**action is enabled.**&nbsp;This is done by passing the&nbsp;`only: [:show]`&nbsp;option to the concern within the&nbsp;`published_posts`&nbsp;resource block, allowing comments to be accessed only when viewing a specific published post.

## A Callable Concern Object

You can implement something more specific to your application with a callable object. In general,&nbsp;**only resort to this option if you have somewhat complicated business logic that's out of place in your routes file**.

The concern object must respond to the&nbsp;`call`&nbsp;method, which receives two parameters:

1. The current mapper object
2. A hash of options to be used by this concern object.

```ruby
# purchasable.rb
class Purchasable
  def initialize(defaults = {})
    @defaults = defaults
  end
        
  def call(mapper, options = {})
    options = @defaults.merge(options)
    mapper.resources :purchases
    mapper.resources :receipts
    mapper.resources :returns if options[:returnable]
  end
end
        
# routes.rb
concern :purchasable, Purchasable.new(returnable: true)
        
resources :toys, concerns: :purchasable
resources :electronics, concerns: :purchasable
resources :pets do
  concerns :purchasable, returnable: false
end
```

You can use any routing helpers you want inside a concern. If using a callable object, they're accessible from the Mapper that's passed to&nbsp;`call`.

