Rails routes with :name instead of :id url parameters

Good answer with Rails 4.0+ :

resources :companies, param: :name

optionally you can use only: or except: list to specify routes

and if you want to construct a URL, you can override ActiveRecord::Base#to_param of a related model:

class Video < ApplicationRecord
  def to_param
    identifier
  end

  # or
  alias_method :to_param, :identifier
end

video = Video.find_by(identifier: "Roman-Holiday")
edit_videos_path(video) # => "/videos/Roman-Holiday"

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)