# Define Custom Routes Using the Member and Collection Blocks in Rails

*Published on October 15, 2022*

---


RESTful routes in Rails provide you with a nice set of named routes which are mapped to the common, standardized URL patterns. However, there might be situations when you want to veer of the beaten path and create custom routes in addition to the RESTful ones. Rails lets you accomplish this using member and collection routes. Let's learn how they work.

**But first, do you really need them at all?**

Before you continue, keep in mind that a better alternative to adding custom, non-resourceful routes is to introduce a new resourceful controller. Not only this keeps your code clean, but gives you better domain model.

Here's what David has to say on this:

> What I’ve come to embrace is that being almost fundamentalistic about when I create a new controller to stay adherent to REST has served me better every single time. Every single time I’ve regretted the state of my controllers, it’s been because I’ve had too few of them. I’ve been trying to overload things too heavily.  
>   
> David Heinemeier Hansson,&nbsp;[Full Stack Radio](https://fullstackradio.com/32)

That said, reality is often messy, and Rails realizes this fact, providing you two ways to add custom, non-resourceful routes for your application.

* * *

### TL;DR

When you need to add more actions to a RESTful resource use&nbsp;`member`&nbsp;and&nbsp;`collection`&nbsp;routes.

```ruby
# bad
get 'articles/:id/preview'
resources :articles

# good
resources :articles do
  get 'preview', on: :member
end

# bad
get 'posts/search'
resources :posts

# good
resources :posts do
  get 'search', on: :collection
end
```

If you want to create more than one&nbsp;`member/collection`&nbsp;routes, use the alternative block syntax.

```ruby
resources :articles do
  member do
    get 'preview'
    get 'search'
    # more routes
  end
end

resources :images do
  collection do
    get 'search'
    get 'preview'
    # more routes
  end
end
```

**Again, do you really need these custom routes at all?**&nbsp;Could you introduce `SearchController` and `PreviewController`&nbsp;with resourceful actions such as&nbsp;`index`,&nbsp;`show`, and&nbsp;`create`&nbsp;instead?

Anyway, let's dig in to learn how you can create custom routes when you absolutely have to.

* * *

A single call to&nbsp;`resources`&nbsp;in the&nbsp;`routes.rb`&nbsp;file declares the seven standard routes for your resource.&nbsp;**What if you need additional routes?**&nbsp;Don't worry. Rails provides the&nbsp;`member`&nbsp;and&nbsp;`collection`&nbsp;blocks so you can define custom routes for both the resource collection and the individual resource.

Here's how you'd typically define routes for the&nbsp;`article`&nbsp;resource.

```ruby
resources :articles
```

This creates the following routes.

```bash
➜ bin/rails routes -g article

      Prefix Verb   URI Pattern                  Controller#Action
    articles GET    /articles(.:format)          articles#index
             POST   /articles(.:format)          articles#create
 new_article GET    /articles/new(.:format)      articles#new
edit_article GET    /articles/:id/edit(.:format) articles#edit
     article GET    /articles/:id(.:format)      articles#show
             PATCH  /articles/:id(.:format)      articles#update
             PUT    /articles/:id(.:format)      articles#update
             DELETE /articles/:id(.:format)      articles#destroy
```

But let's say you are writing your articles in markdown, and need to see a preview of the article as you write it.

You could create a&nbsp;`PreviewController`&nbsp;and display the article's preview using its&nbsp;`show`&nbsp;action, but it's convenient to add a preview action on the&nbsp;`ArticlesController`&nbsp;itself.

## Custom Member Routes

Here's how you define the&nbsp;`preview`&nbsp;route on the&nbsp;`ArticlesController`&nbsp;using the&nbsp;`member`&nbsp;block.

```ruby
resources :articles do
  member do
    get 'preview'
  end
end
```

The Rails router adds a new route that directs the request to&nbsp;`ArticlesController#preview`&nbsp;action. The remaining routes remain unchanged. It also passes the article id in&nbsp;`params[:id]`&nbsp;and creates the&nbsp;`preview_article_path`&nbsp;and&nbsp;`preview_article_url`&nbsp;helpers.

```bash
➜ bin/rails routes -g article

         Prefix Verb   URI Pattern                     Controller#Action
preview_article GET    /articles/:id/preview(.:format) articles#preview

...remaining routes
```

If you have a single&nbsp;`member`&nbsp;route, use the short-hand version by passing the&nbsp;`:on`&nbsp;option to the route, eliminating the block.

```ruby
resources :articles do
  get 'preview', on: :member
end
```

You can go one step further and leave out the&nbsp;`:on`&nbsp;option.

```ruby
resources :articles do
  get 'preview'
end
```

It generates the following route.

```bash
➜  bin/rails routes -g preview

         Prefix Verb URI Pattern                         Controller#Action         
article_preview GET  /articles/:article_id/preview(.:format) articles#preview
```

There are two important differences here:

1. The article's id is available as&nbsp;`params[:article_id]`&nbsp;instead of&nbsp;`params[:id]`.
2. The route helpers changes from&nbsp;`preview_article_path`&nbsp;to&nbsp;`article_preview_path`&nbsp;and&nbsp;`preview_article_url`&nbsp;to&nbsp;`article_preview_url`.

## Custom Collection Routes

To add a new route for the collection of a resource, use the&nbsp;`collection`&nbsp;block.

```ruby
resources :articles do
  collection do
    get 'search'
  end
end
```

This adds the following new route. It will also add a&nbsp;`search_articles_path`&nbsp;and&nbsp;`search_articles_url`&nbsp;helper.

```bash
search_articles GET    /articles/search(.:format)   articles#search
```

If you don't need multiple&nbsp;`collection`&nbsp;routes, just pass&nbsp;`:on`&nbsp;option to the route.

```ruby
resources :articles do
  get 'search', on: :collection
end
```

This will add the same route as above.

## Conclusion

Rails allows you to break out of its convention of using seven resourceful routes using the&nbsp;`member`&nbsp;and&nbsp;`collection`&nbsp;blocks. Both allow you to define additional routes for your resources than the standard seven routes. A&nbsp;`member`&nbsp;block acts on a single member of the resource, whereas a&nbsp;`collection`&nbsp;operates on a collection of that resource.

