---
title: "Rails 7.1 adds new option path_params for url_for helper method"
description: "Rails 7.1 adds new option path_params for url_for helper method"
canonical_url: "https://www.bigbinary.com/blog/url-for-path-params-options"
markdown_url: "https://www.bigbinary.com/blog/url-for-path-params-options.md"
---

# Rails 7.1 adds new option path_params for url_for helper method

Rails 7.1 adds new option path_params for url_for helper method

- Author: Neenu Chacko
- Published: November 7, 2023
- Categories: Rails, Rails 7

With Rails 7.1, the `url_for` helper now supports a new option called
`path_params`.

Prior to Rails 7.1, if you had your routes configured to be scoped under say,
`user_id` as shown below, you would have to explicitly specify the `user_id` in
every single place where you want to generate a link for the scoped routes, such
as when writing view files.

```ruby
#routes.rb
Rails.application.routes.draw do
  root "articles#index"

  scope ":user_id" do
    get "/articles", to: "articles#index"
    get "/articles/:id", to: "articles#show"
  end

  get "/categories", to: "categories#index"
end
```

```html
<!-- app/views/articles/index.html.erb -->

<a href="<%= articles_path(user_id: @current_user.id) %>"> Articles </a>
```

This could be solved by updating `ApplicationController` and overwriting the
`default_url_options` method:

```ruby
# application_controller.rb
class ApplicationController < ActionController::Base
  def default_url_options
    { user_id: "unique-id" }
  end
end
```

The `default_url_options` method is used to overwrite and set default options
for all the methods based on `url_for`. However, this meant that all routes,
even those that did not belong to the `user_id` scope, would have the
`?user_id=unique-id` query param added to the end of them, resulting in the
following output:

```ruby
articles_path # => /unique-id/articles
categories_path # => /categories?user_id=unique-id
```

Rails 7.1 fixes this issue with the addition of the `path_params` option. If you
pass a hash of parameters to this key, those parameters will only be used for
the named segments of the route. If they aren't used, they are discarded instead
of being appended to the end of the route as query params.

With this change, we can implement the `default_url_options` method as follows:

```ruby
class ApplicationController < ActionController::Base
  def default_url_options
    { path_params: { user_id: "unique-id" } }
  end
end
```

The `url_for` helper method will now give you the following output:

```ruby
articles_path # => /unique-id/articles
articles_path(user_id: "test-id") # => /test-id/articles
categories_path # => /categories
categories_path(user_id: "test-id") # => /categories
```

The view file can now be written as:

```html
<a href="<%= articles_path %>"> Articles </a>
```

This is very useful in situations where you only want to add a required param
that is part of the route's URL without introducing unnecessary query params for
other routes.

Please check out this [pull request](https://github.com/rails/rails/pull/43770)
for more details.

## Links

- [Human page](https://www.bigbinary.com/blog/url-for-path-params-options)
