# Polymorphic URLs with direct Router Helper

*Published on July 08, 2025*

---


While reading the source code for the&nbsp;[Maybe](https://github.com/maybe-finance/maybe)&nbsp;project (which is a really good Rails codebase that follows most of the Rails best practices and conventions), I came across this code in the router config file. It uses the Rails routing feature called&nbsp;`direct`&nbsp;to create custom URL helpers for a polymorphic model.

```ruby
direct :entry do |entry, options|
  if entry.new_record?
    route_for entry.entryable_name.pluralize, options
  else
    route_for entry.entryable_name, entry, options
  end
end
```

At first glance, I didn't understand what it was doing, as I've personally never had to use the&nbsp;`direct`&nbsp;method like this in my Rails projects so far, so decided to do some reading and wanted to share everything I learned.

**First, what is the**&nbsp;**`direct`**&nbsp;**method in the Rails router?**

The&nbsp;`direct`&nbsp;helper lets you defined custom named route helpers that generate URLs based on custom logic, rather than the default resourceful routes. You can then use these helpers throughout your application, just like any other URL helpers in Rails.

> It's particularly useful when the URL for a model doesn't follow the standard conventions that resourceful routing handles automatically.

In the simplest case, you can simply return a URL that you'd like to use when that custom URL helper is used. Consider the following example.

```ruby
direct :course_landing_page do
  "https://writesoftwarewell.com/courses"
end
```

This will generate following URL helper methods

- `course_landing_page_url`&nbsp;returns&nbsp;https://writesoftwarewell.com/courses
- `course_landing_page_path`&nbsp;returns&nbsp;`/courses`.

If you pass any arguments to the helper, they get forwarded to the direct block.

```ruby
direct :greeting_page do |user|
  "https://writesoftwarewell.com/greet/#{user.name}"
end
```

This generates the helpers&nbsp;`greeting_page_url`&nbsp;and&nbsp;`greeting_page_path`. Assuming&nbsp;`matz.name`&nbsp;returns&nbsp;`"yukihiro"`, you can use them as follows:

- `greeting_page_url(matz)`&nbsp;returns&nbsp;https://writesoftwarewell.com/greet/yukihiro
- `greeting_page_path(matz)`&nbsp;returns&nbsp;`greet/yukihiro`&nbsp;.

To learn more about the basic usage of the direct method, check out the following post: [Custom URL Helpers in Rails with the direct Method](/direct-custom-url-helpers-rails)

The&nbsp;`direct`&nbsp;method is pretty flexible and you can encapsulate complex or polymorphic path logic in the block, which is what the Maybe router is doing.

```ruby
direct :entry do |entry, options|
  if entry.new_record?
    route_for entry.entryable_name.pluralize, options
  else
    route_for entry.entryable_name, entry, options
  end
end
```

This code defines a custom route helper called&nbsp;`entry_path`&nbsp;and&nbsp;`entry_url`. When you call&nbsp;`entry_path(entry)`, where&nbsp;`entry`&nbsp;can be a&nbsp;[polymorphic object](https://guides.rubyonrails.org/association_basics.html#polymorphic-associations)&nbsp;or a&nbsp;[delegated type](https://api.rubyonrails.org/classes/ActiveRecord/DelegatedType.html), Rails will use this block to figure out what URL path to return for the given&nbsp;`entry`&nbsp;object.

Let's assume that&nbsp;`Entry`&nbsp;is a polymorphic model and can be a&nbsp;`Transaction`,&nbsp;`Account`, or&nbsp;`Trade`, etc.

**What does the block do?**

- If the entry is a new record, i.e.&nbsp;`entry.new_record?`&nbsp;returns&nbsp;`true`, Rails will return the value generated by the following code:

```ruby
route_for entry.entryable_name.pluralize, options
```

This means, generate the path for the pluralized entryable name, e.g. if&nbsp;`entry.entryable_name`&nbsp;is "transaction", Rails will return the value of generated by calling&nbsp;`route_for "transactions", options`&nbsp;which ultimately generates the path for the transactions resource, i.e.&nbsp;`transactions_path`, which is&nbsp;`/transactions`.

I'm guessing it's used for the "new" forms, where you want to POST to the collection.

Example:

```ruby
entry = Entry.new(entryable: Transaction.new)
entry_path(entry)

# => transactions_path, i.e. "/transactions"
```

Since the entry was not saved, it routed to the collection path for the entryable type.

- If the entry is persisted (the else condition), it will return the value generated by this code:

```ruby
route_for entry.entryable_name, entry, options
```

This means, generate the path for the singular entryable name and pass the entry as the argument to the helper, i.e.&nbsp;`transaction_path(entry)`

Example:

```javascript
entry = Entry.find(1) # assume this entry is a transaction
entry_path(entry)

# => transaction_path(entry), i.e. "/transactions/1"
```

Since the entry was saved, it routed to the member path for the entryable type.

## **Why use this?**

If you have a polymorphic model that can represent multiple types, a single helper can generate the correct path for any type.

In Maybe, an&nbsp;`Entry`&nbsp;is a polymorphic model that can be one of several types, such as a&nbsp;`Transaction`,&nbsp;`Trade`, or&nbsp;`Valuation`. The URL for an entry should depend on what kind of entry it is.

In addition, the logic forks based on whether the entry has been saved to the database. This is a common pattern in most web applications, where you have to use a different URL in forms, where a new object should POST to a collection URL&nbsp;`/transactions`, while a form for an existing object should PATCH to a member URL, like&nbsp;`/transactions/123`.

### How to use it?
```javascript
<%= link_to "View entry", entry_path(entry) %>
```

This will generate the correct path for the given entry, whether it's a new entry or a persisted entry, regardless of its underlying type.

The following table summarizes it.

![direct-route-helper.png](https://writesoftwarewell.com/media/o8ehpwh4f1dfywva9kye71tj1i3r.png "Direct Route Helper")

Basically, you can use the direct helper method to handle URL generation for polymorphic types. Creating a single URL helper method that hides the complexity of figuring out the type of the entry and whether the entry is new or an existing record.

