RSS Amplifier

Mintbit · Dec 17, 2025

The Difference Between resource and resources in Rails

0
Sign in to vote or save

Mintbit

Rails provides two closely related routing helpers — resource and resources — and although they look similar, they serve different purposes. Understanding the distinction between them is essential for designing clean, intention-revealing routes in a Rails application.

What Are resource and resources?

Both helpers are used in config/routes.rb to generate RESTful routes. The key difference lies in whether the resource is singular or plural, and that directly affects the URLs and controller actions Rails creates for you.

resources: Plural Resources

resources is the most commonly used helper. It represents a collection of objects, where each item has its own identifier (usually an id).

Example:

1
resources :articles

This generates the full set of RESTful routes:

HTTP Verb Path Controller#Action
GET /articles articles#index
GET /articles/new articles#new
POST /articles articles#create
GET /articles/:id articles#show
GET /articles/:id/edit articles#edit
PATCH /articles/:id articles#update
DELETE /articles/:id articles#destroy

You use resources when:

  • There are many records
  • Each record is accessed by an ID
  • You need collection-level actions like index

Typical examples include posts, comments, products, and users.

resource: Singular Resources

resource (singular) represents a single object that does not need an ID in the URL. Rails assumes there is only one instance of that resource per context (often per user).

Example:

1
resource :profile

This generates a smaller set of routes:

HTTP Verb Path Controller#Action
GET /profile/new profiles#new
POST /profile profiles#create
GET /profile profiles#show
GET /profile/edit profiles#edit
PATCH /profile profiles#update
DELETE /profile profiles#destroy

Notice what’s missing:

  • No index route
  • No :id segment in the URL

You use resource when:

  • There is only one instance
  • The resource is implicitly identified
  • An id would be redundant

Common examples include user profiles, dashboards, shopping carts, or account settings.

Controller Naming Still Uses Plural

Even when using resource, Rails still expects a plural controller name:

1
2
resource :profile
# maps to ProfilesController

This keeps controller naming consistent across the framework.

Choosing the Right One

A good rule of thumb:

  • Use resources when you can say: “There are many of these.”
  • Use resource when you can say: “There is only one of these per scope.”

For example:

  • A user has many posts → resources :posts
  • A user has one profile → resource :profile

Why This Matters

Choosing between resource and resources is more than syntax — it communicates intent. Singular routes make your URLs cleaner, your controllers simpler, and your application easier to reason about.

Read the original on mintbit.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.