Skip to content

Instantly share code, notes, and snippets.

@Georgy5
Created March 12, 2023 10:02
Show Gist options
  • Save Georgy5/fd15302df3b9251e03e52d00cd8438f1 to your computer and use it in GitHub Desktop.
Save Georgy5/fd15302df3b9251e03e52d00cd8438f1 to your computer and use it in GitHub Desktop.
Rails SEO friendly URLs
# It's super easy to create SEO friendly URLs (I love that it just works!)
def to_param
"#{id}-#{title.parameterize}"
end
## Alternative implementation of the #to_param method from the book
## Learn Rails 6 by Adam Notodikromo
## Change the default :id parameter
# config/routes.rb
resources :user, param: :username
# app/models/user.rb
def to_param
username
end
## Suggestion: Use a sensible exclude list to prevent users from having usernames like admin, support, official, secure, etc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment