【Rails】/users/:id ではなく/:username にする


理想


やり方

to_paramを使用する

user.rb
class User < ActiveRecord::Base
  validates_presence_of :username
  validates_uniqueness_of :username, case_sensitive: false

  def to_param
    username
  end

users_controller.rb
class UsersController < ApplicationController
  def show
    @user = User.find_by(username: params[:id])
  end
end
routes.rb
  resources: users, path: '/', only: [:show]