This blog is part of our Rails 6 series. Rails 6.0 was recently released.

The :param option in routes is used to override default resource identifier i.e. :id.

Let’s take for an example that we want product :name to be as the default resource identifier instead of :id while defining routes for products . In this case, :param option comes handy. We will see below how we can use this option.

Before Rails 6, if resource custom param contains a colon, Rails used to consider that as an extra param which should not be the case because it sneaks in an extra param.

An issue was raised in Aug, 2017 which was later fixed in February this year.

So, now Rails 6 raises ArgumentError if a resource custom param contains a colon(:).

Let’s checkout how it works.

Rails 5.2

Let’s create routes for products with custom param as name/:pzn .

>> Rails . application . routes . draw do >> resources :products , param: 'name/:pzn' >> end

$ rake routes | grep products products GET /products(.:format) products#index POST /products(.:format) products#create new_product GET /products/new(.:format) products#new edit_product GET /products/:name/:pzn/edit(.:format) products#edit product GET /products/:name/:pzn(.:format) products#show PATCH /products/:name/:pzn(.:format) products#update PUT /products/:name/:pzn(.:format) products#update DELETE /products/:name/:pzn(.:format) products#destroy

As we can see, Rails also considers :pzn as a parameter.

Now let’s see how it works in Rails 6.

Rails 6.0.0.rc1

>> Rails . application . routes . draw do >> resources :products , param: 'name/:pzn' >> end

$ rake routes | grep products rake aborted! ArgumentError: :param option can't contain colons /Users/amit/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.0.rc1/lib/action_dispatch/routing/mapper.rb:1149:in `initialize' /Users/amit/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.0.rc1/lib/action_dispatch/routing/mapper.rb:1472:in `new' /Users/amit/.rvm/gems/ruby-2.6.3/gems/actionpack-6.0.0.rc1/lib/action_dispatch/routing/mapper.rb:1472:in `block in resources' ... ... ...

Here is the relevant issue and the pull request.