However, we still need the ID at the start of our parameterized URL value for Rails ActiveRecord to find the correct model instance. If we don’t want IDs to be presented in URLs at all, the friendly_id gem is a great choice for this.
How to install and use friendly_id in your rails project.
Here we also install babosa for multiple languages.
123
# add this to your Gemfilegem'friendly_id'gem'babosa'# this is for locale sensitive transliteration, with support for many languages.
12345678910
# in rails project$ rails generate friendly_id
$ rails generate migration add_slug_to_models slug:string
$ rake db:migrate
#in rails consoleIf all have been already done, we should run rails console and save all our model records again.
> Model.find_each(&:save)
modify our rails model
123456789101112131415161718192021
# edit app/models/model.rbclassModel<ActiveRecord::BaseextendFriendlyIdfriendly_id:column_name,use::slugged# :history => won't change friendly id after updating :column_name# :finder => no need to add .friendly method with Model anymore (gem's version should be above v5.0)# use: [:slugged, :history, :finder]# for genereate friendly_iddefshould_generate_new_friendly_id?new_record?||slug.blank?end# for locale sensitive transliteration with friendly_iddefnormalize_friendly_id(input)input.to_s.to_slug.normalize.to_sendend