Do you want the company name to appear in the /companies/ part of the url or the /:company_id/ part?
I presume the intention is to have more relevant urlâs in your app. By far the easiest way is to modify a model by overriding the to_param method. When Rails wants to represent a model in the url it calls to_param. We can override this to include the company name:
class Company < ActiveRecord::Base
...
def to_param
"#{id}-#{name}".parameterize
end
end
We call .parameterize to ensure the name is url friendly. Now when you visit a company page it will show the name (e.g. /companies/1-thoughtbot/). By keeping the id in the url we donât need to make any changes to how rails retrieves the company record. In the controller when we call Company.find(params[:id]) ActiveRecord will call to_int on it to convert into an integer. This is why itâs necessary to have the id at the start of this part of the URL.
If you donât want the idâs showing then youâll need to get more advance. Iâd highly recommend checking out Rails Casts. One of the episodes available to subscribers is on this very topic