Created
March 12, 2023 10:02
-
-
Save Georgy5/fd15302df3b9251e03e52d00cd8438f1 to your computer and use it in GitHub Desktop.
Rails SEO friendly URLs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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