# Working with Resourceful Routes in Ruby on Rails

*Published on August 02, 2024*

Tags: Router

---


> _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 controller, it's been because I've had too few of them. I've been trying to overload things too heavily._  
>   
> _- David Heinemeier Hansson, on_&nbsp;[_Full Stack Radio_](https://fullstackradio.com/32)

Watch David's RailsConf 2006 keynote: [Resources on Rails](https://www.youtube.com/watch?v=GFhoSMD6idk)

The concept of resources is very powerful in Rails. With a single call to a method named&nbsp;`resources`, Rails will generate seven different routes for you, saving you a lot of typing.

But saving a few keystrokes is just the cherry on top. The biggest benefit of using resourceful routing is that it provides&nbsp;**a nice organizational structure for your Rails application**, and helps you figure out&nbsp;**how to name and organize the controller classes and action methods**.

Not only that, resourceful routing imposes certain constraints on your project which make most Rails applications consistent and familiar. When a developer says that they have a post resource, another can safely assume (most of the time) that they have a&nbsp;`PostController`&nbsp;class with actions like&nbsp;`create, show, edit, new, ...`&nbsp;along with a&nbsp;`Post`&nbsp;model saved in the&nbsp;`posts`&nbsp;table in the database. This&nbsp;[conceptual compression](https://www.youtube.com/watch?v=zKyv-IGvgGE&t=1037s)&nbsp;is not to be taken lightly.

Since the concepts of REST and Resource are such an integral part of all Rails applications, let's try to understand them in depth.

💡 This post is part of my&nbsp;[handbook on the Rails Router](/tag/router).

## **What's a Resource?**

A resource, like an object, is one of those concepts that's difficult to put into words. You intuitively understand them, but you can't express them. I'll stick to how&nbsp;[Roy Fielding](https://roy.gbiv.com/), the inventor of REST, defined them in his&nbsp;[dissertation](https://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm).

> 💡 A resource&nbsp;_R_&nbsp;is a temporally varying membership function&nbsp;_M_R_(t)_, which for time&nbsp;_t_&nbsp;maps to a set of entities, or values, which are equivalent.

Is your head spinning? 🤯 Mine too. Let's try the other definition he gives in the same section.

> 💡 A resource is the key abstraction of information. Any information that can be named can be a resource.

Much better, right? I think that definition captures the essence of resources really well.

Any concept in your application domain can be a resource: an article, a course, or an image. It doesn't have to be a physical entity, but can be an abstract concept such as a weather forecast or an SSH connection to connect to a remote server.

The main point is:&nbsp;**you have total freedom**&nbsp;in choosing whatever idea you are using in your application as a resource.

**How to Resources Relate to REST?**

Now, you may have heard the term&nbsp;**REST**&nbsp;(Representational State Transfer). In simple terms, REST is a way for different machines on the Internet to communicate with each other using resources.

It's important to remember that you only get&nbsp;**a representation of the resource**&nbsp;(hence the name 'Representational State Transfer'). For example, if you ask the HTML representation of a Post resource ([like Hotwire or traditional web apps](/introduction-to-hotwire)), you'll get the post as an HTML. If you ask for a JSON representation (like many SPA frameworks do), you'll get the post JSON. Makes sense?

**But Why do I need a Resource?**

Good question. You might be wondering what this discussion of resources has to do with routing. Well, resources are fundamental to routing. They allow you to&nbsp;**reduce seven different routes into a single method call.**&nbsp;Let's learn how.

### **Identifying Patterns**

If you've used any software application, you must have noticed that most of them have a few common patterns. Most of the time, you're:

1. **Creating**&nbsp;a resource (publishing a post, uploading an image)
2. **Reading**&nbsp;that resource (viewing the tweets, listening to podcasts)
3. **Updating**&nbsp;the resource (changing source code on GitHub, editing the post)
4. **Deleting**&nbsp;the resource (removing a folder from Dropbox, deleting your profile picture)

No matter how fancy your application is, I guarantee it will have some form of these four actions that your users can take. Collectively, they're called&nbsp;**CRUD**&nbsp;which stands for&nbsp;_**Create, Read, Update**_**, and**&nbsp;_**Delete**_**.**

> 💡 A resource is an object that you want users to be able to access via URI and perform&nbsp;[CRUD](http://en.wikipedia.org/wiki/Create,_read,_update_and_delete)&nbsp;operations.

For each of these actions, your application should have a route. Assuming you're building a course platform, your routes file might have the following routes.

```ruby
post   'courses'      => 'courses#create'
get    'course'       => 'course#show'
patch  'courses/:id'  => 'courses#update'
delete 'courses/:id'  => 'courses#destroy'
```

In addition to these four routes, you'll typically have three more:

- one route to see all the courses

```ruby
get 'courses'          => 'courses#index'
```

- two routes to fetch the pages that allow the users create and modify the courses. These are different from the routes that actually save and update the courses.

```ruby
get 'courses/new'      => 'courses#new'
get 'courses/:id/edit' => 'courses#edit'
```

So we have a total of seven routes that are common to most resources in most web applications.

- If you are building a blog, you'll have these seven routes for posts.
- If you're building Instagram, you'll have the same routes for images.
- If you're building a course platform, you'll have these routes for courses and lessons.

**No matter the topic, you'll most likely have some subset of these seven routes for each resource.**

Since this pattern is so common and repetitive, Rails introduced the concept of resourceful routing, whereby calling one method named&nbsp;`resources`&nbsp;and passing the plural name of the resource, i.e.&nbsp;`:posts`,&nbsp;`:courses`,&nbsp;`:images`, etc. you get these seven routes for free. Rails will automatically generate them for you.

```ruby
Rails.application.routes.draw do
  resources :courses
  resources :photos
  resources :posts
end
```

In addition, resourceful routing also generates sensible names for these routes. For example, you'll have names like&nbsp;`course_path`,&nbsp;`edit_course_path`&nbsp;at your disposal without providing the&nbsp;`:as`&nbsp;option.

The following table summarizes what you get with a single call to&nbsp;`resources :courses`.

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

     Prefix Verb   URI Pattern                 Controller#Action
    courses GET    /courses(.:format)          courses#index
            POST   /courses(.:format)          courses#create
 new_course GET    /courses/new(.:format)      courses#new
edit_course GET    /courses/:id/edit(.:format) courses#edit
     course GET    /courses/:id(.:format)      courses#show
            PATCH  /courses/:id(.:format)      courses#update
            PUT    /courses/:id(.:format)      courses#update
            DELETE /courses/:id(.:format)      courses#destroy
```

The values under the&nbsp;**Prefix**&nbsp;column represent the prefix of the route's name, e.g.&nbsp;`new_course`&nbsp;gives you&nbsp;`new_course_path`&nbsp;and&nbsp;`new_course_url`&nbsp;helpers, and so on. The rows with empty prefixes just follow the ones from the above row.

In addition to having multiple resources, there's also a singular form of resourceful routes. It represents a resource that only has one, single form. For example, a logged-in user's profile. You can use the&nbsp;`resource`&nbsp;method for this.

```ruby
resource :profile
```

It creates the following routes:

```bash
 new_profile GET    /profile/new(.:format)  profiles#new
edit_profile GET    /profile/edit(.:format) profiles#edit
     profile GET    /profile(.:format)      profiles#show
             PATCH  /profile(.:format)      profiles#update
             PUT    /profile(.:format)      profiles#update
             DELETE /profile(.:format)      profiles#destroy
             POST   /profile(.:format)      profiles#create
```

Note two important differences from the plural version:

1. The route to show all profiles is missing. Since we only have a single profile, there's no point in displaying all.
2. None of the routes contain an&nbsp;`id`&nbsp;segment key. We don't need it to identify a profile as there's only one profile.

**What if I don't have all seven routes?**

Sometimes, you don't want all seven routes that&nbsp;`resources`&nbsp;method creates for you. In such cases, you can pass the&nbsp;`only`&nbsp;or&nbsp;`except`&nbsp;options to filter the ones you need or don't need.

```ruby
resources :courses, only: [:index, :show]

resources :courses, except: [:delete]
```

You can even nest multiple resources, but I'll leave that topic for a different post.

**Can I define more routes in addition to the seven resourceful routes?**

Yes, Rails provides you the&nbsp;`member`&nbsp;and&nbsp;`collection`&nbsp;helpers to define non-conventional routes. Check out the following post to learn more.

[Define Custom Routes Using the Member and Collection Blocks in Rails](/define-new-routes-using-member-and-collection-blocks-in-rails)

That said, any time you find yourself reaching for non-resourceful routes, stop and consider if you need a separate resource with a dedicated controller instead.

## Conclusion

**To summarize**&nbsp;what we've learned so far,&nbsp;any concept in your application domain can be a resource:&nbsp;**an article**,&nbsp;**a course**,&nbsp;**or an image**, even a&nbsp;**user session**. It doesn't have to be a physical entity, but anything that can be named.

Resourceful routing allows you to quickly declare all of the common routes for a given resource. A single call to&nbsp;`resources`&nbsp;declares seven routes for&nbsp;`index`,&nbsp;`show`,&nbsp;`new`,&nbsp;`create`,&nbsp;`edit`,&nbsp;`update`, and&nbsp;`destroy`&nbsp;actions.

Using resourceful routing not only structures your Rails codebase but also provides a nice organizational structure, answering the common questions:&nbsp;**what to name this controller or method.**&nbsp;So put it to good use, and only resort to non-resourceful routes when you have to.

