# How to Override the Named Route Parameter ID in Rails

*Published on July 12, 2024*

Tags: Router

---


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

In the previous posts, we've learned about the concepts of dynamic segments as well as named routes in Rails. Naming a route generates two helper methods which can be used to generate the route URL, whereas a dynamic segment allows us to create dynamic routes, where the parts of the URL can vary.

In this post, we'll combine these two concepts. Consider the following named route which contains a dynamic segment called&nbsp;`:id`:

```javascript
# config/routes.rb
get "item/:id", to: "items#show", as: "item"
```

When the application receives a request on&nbsp;`/item/4`, it forwards it to the `ItemsController#show` action. You can access the ID of the post as follows:&nbsp;`params[:id]`.

To generate the item URL, you can use the named route helper methods&nbsp;`item_path`&nbsp;and&nbsp;`item_url`, passing the ID of the post.

```javascript
item_path(id: @item.id) # /item/10
```

In this example,&nbsp;`@item`&nbsp;is an instance of a Rails model (don't worry if you don't know what a model is, we'll learn it in detail later).

Typically, most of the time you'll work with the&nbsp;`id`&nbsp;of a model. To make this convenient, Rails makes the above named helper method call even shorter. Just pass the id, without the&nbsp;`:id`&nbsp;key.

```javascript
item_path(@item.id)  # /item/10
```

You can go even one step further. Simply pass the instance, and Rails will figure out the&nbsp;`id`&nbsp;from the object.

```javascript
item_path(@item)
```

It's recommended that you use the last syntax as not only it's shorter, but also it's very readable. Just reading&nbsp;`item_path(@item)`&nbsp;tells us that we need the path to an item.

At this point, you may be wondering: How does Rails know to call the&nbsp;`id`&nbsp;method on the passed instance, i.e.&nbsp;`@item`?

**Answer:**&nbsp;Behind the scenes, Rails calls the&nbsp;`to_param`&nbsp;method on the provided object. The&nbsp;`to_param`&nbsp;method is defined on all ActiveRecord models, and it returns the&nbsp;`id`&nbsp;of the model by default. That's how Rails knows to find the proper ID of the model when you pass it directly to the helper method, i.e.&nbsp;`item_path(@item)`.

If there are multiple dynamic segments in the route, you can provide multiple corresponding models.

```javascript
get "section/:section_id/article/:id", to: "articles#show", as: "section_article"

section = Section.first
article = session.articles.first
section_article_url(section, article) # /section/1/article/2
```

You must provide the model arguments to the helper method in the same order in which their IDs occur in the dynamic segments.

**So here's what we've learned so far:**&nbsp;Rails will infer the model ID passed to the named route helper method. It does so by calling the&nbsp;`to_param`&nbsp;method on the provided model instance, which returns the model's&nbsp;`id`&nbsp;by default.

That raises the question, can we customize the value of the dynamic segment to use another value, instead of using the default model&nbsp;`id`&nbsp;value? We can. Simply override the&nbsp;`id`&nbsp;value by defining a&nbsp;`to_param`&nbsp;method in your model and returning the value you want to insert.

For example, instead of an&nbsp;`id`, you want to use the slug of a post to identify it, i.e. instead of&nbsp;`/posts/10`&nbsp;where 10 is the ID of the post, you want&nbsp;`/posts/ruby-on-rails`&nbsp;where&nbsp;`ruby-on-rails`&nbsp;is the slug of the post titled "Ruby on Rails" (a slug is typically the lowercased, dasherized version of the title).

To achieve this, you will override the&nbsp;`to_param`&nbsp;method in the&nbsp;`Post`&nbsp;model as follows:

```javascript
class Post < ApplicationRecord
  def to_param
    title.parameterize
  end
end
```

After this, whenever you call the named helper methods&nbsp;`post_path(post)`, or&nbsp;`post_url(post)`&nbsp;it will produce the URL containing the slug of the post.

```javascript
@post = Post.create(title: "Ruby on Rails")

# in view
post_path(@post) # /posts/ruby-on-rails
```

Finally, you can update the dynamic segment from&nbsp;`:id`&nbsp;to&nbsp;`:slug`, so that you can use&nbsp;`params[:slug]`&nbsp;in the controller to extract the slug from the URL.

```javascript
get "posts/:slug", to: "posts#show", as: "post"
```

