How to slugify a string in Ruby on Rails

How to slugify a string in Ruby on Rails

/ #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

EFSArCBy | Jul 20, 26 08:33

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

-1 OR 2+951-951-1=0+0+0+1 --

EFSArCBy | Jul 20, 26 08:34

-1 OR 2+450-450-1=0+0+0+1

EFSArCBy | Jul 20, 26 08:34

-1' OR 2+648-648-1=0+0+0+1 --

EFSArCBy | Jul 20, 26 08:34

-1' OR 2+376-376-1=0+0+0+1 or 'JCZUtqV1'='

EFSArCBy | Jul 20, 26 08:34

-1" OR 2+691-691-1=0+0+0+1 --

EFSArCBy | Jul 20, 26 08:34

555*if(now()=sysdate(),sleep(15),0)

EFSArCBy | Jul 20, 26 08:34

5550'XOR(555*if(now()=sysdate(),sleep(15),0))XOR'Z

EFSArCBy | Jul 20, 26 08:34

5550"XOR(555*if(now()=sysdate(),sleep(15),0))XOR"Z

EFSArCBy | Jul 20, 26 08:34

(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/

EFSArCBy | Jul 20, 26 08:34

555-1; waitfor delay '0:0:15' --

EFSArCBy | Jul 20, 26 08:34

555-1); waitfor delay '0:0:15' --

EFSArCBy | Jul 20, 26 08:34

555-1 waitfor delay '0:0:15' --

EFSArCBy | Jul 20, 26 08:34

5552gCVNMET'; waitfor delay '0:0:15' --

EFSArCBy | Jul 20, 26 08:34

555-1 OR 946=(SELECT 946 FROM PG_SLEEP(15))--

EFSArCBy | Jul 20, 26 08:34

555-1) OR 421=(SELECT 421 FROM PG_SLEEP(15))--

EFSArCBy | Jul 20, 26 08:34

555-1)) OR 927=(SELECT 927 FROM PG_SLEEP(15))--

EFSArCBy | Jul 20, 26 08:34

555TOF4teil' OR 443=(SELECT 443 FROM PG_SLEEP(15))--

EFSArCBy | Jul 20, 26 08:34

555Qm9R6BVf') OR 349=(SELECT 349 FROM PG_SLEEP(15))--

EFSArCBy | Jul 20, 26 08:34

555cJbJBpKF')) OR 658=(SELECT 658 FROM PG_SLEEP(15))--

EFSArCBy | Jul 20, 26 08:34

555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)

EFSArCBy | Jul 20, 26 08:34

555'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555'"

EFSArCBy | Jul 20, 26 08:34

@@1G4Kz

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

-1 OR 2+600-600-1=0+0+0+1 -- | Jul 20, 26 08:34

555

-1 OR 2+454-454-1=0+0+0+1 | Jul 20, 26 08:34

555

-1' OR 2+487-487-1=0+0+0+1 -- | Jul 20, 26 08:34

555

-1' OR 2+559-559-1=0+0+0+1 or 'DhEuyg3o'=' | Jul 20, 26 08:34

555

-1" OR 2+534-534-1=0+0+0+1 -- | Jul 20, 26 08:34

555

if(now()=sysdate(),sleep(15),0) | Jul 20, 26 08:34

555

EFSArCBy0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z | Jul 20, 26 08:34

555

EFSArCBy0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z | Jul 20, 26 08:34

555

EFSArCBy-1 waitfor delay '0:0:15' -- | Jul 20, 26 08:34

555

EFSArCBy6b2T7YGG'; waitfor delay '0:0:15' -- | Jul 20, 26 08:34

555

EFSArCByGkfLXWvm' OR 609=(SELECT 609 FROM PG_SLEEP(15))-- | Jul 20, 26 08:34

555

EFSArCBynZnX7IQj') OR 489=(SELECT 489 FROM PG_SLEEP(15))-- | Jul 20, 26 08:34

555

EFSArCByUtQTQT1k')) OR 62=(SELECT 62 FROM PG_SLEEP(15))-- | Jul 20, 26 08:34

555

EFSArCBy'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||' | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy'" | Jul 20, 26 08:34

555

@@K9ord | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 20, 26 08:34

555

EFSArCBy | Jul 23, 26 11:21

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

-1 OR 2+516-516-1=0+0+0+1 --

EFSArCBy | Jul 23, 26 11:23

-1 OR 2+724-724-1=0+0+0+1

EFSArCBy | Jul 23, 26 11:23

-1' OR 2+899-899-1=0+0+0+1 --

EFSArCBy | Jul 23, 26 11:23

-1' OR 2+400-400-1=0+0+0+1 or 'vZbMlMCE'='

EFSArCBy | Jul 23, 26 11:23

-1" OR 2+259-259-1=0+0+0+1 --

EFSArCBy | Jul 23, 26 11:23

555*if(now()=sysdate(),sleep(15),0)

EFSArCBy | Jul 23, 26 11:23

5550'XOR(555*if(now()=sysdate(),sleep(15),0))XOR'Z

EFSArCBy | Jul 23, 26 11:23

5550"XOR(555*if(now()=sysdate(),sleep(15),0))XOR"Z

EFSArCBy | Jul 23, 26 11:23

(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/

EFSArCBy | Jul 23, 26 11:23

555-1; waitfor delay '0:0:15' --

EFSArCBy | Jul 23, 26 11:23

555-1); waitfor delay '0:0:15' --

EFSArCBy | Jul 23, 26 11:23

555-1 waitfor delay '0:0:15' --

EFSArCBy | Jul 23, 26 11:23

555cwRYF0pK'; waitfor delay '0:0:15' --

EFSArCBy | Jul 23, 26 11:23

555-1 OR 843=(SELECT 843 FROM PG_SLEEP(15))--

EFSArCBy | Jul 23, 26 11:23

555-1) OR 841=(SELECT 841 FROM PG_SLEEP(15))--

EFSArCBy | Jul 23, 26 11:23

555-1)) OR 507=(SELECT 507 FROM PG_SLEEP(15))--

EFSArCBy | Jul 23, 26 11:23

555EHJn9SMX' OR 830=(SELECT 830 FROM PG_SLEEP(15))--

EFSArCBy | Jul 23, 26 11:23

555a4PYxRHz') OR 645=(SELECT 645 FROM PG_SLEEP(15))--

EFSArCBy | Jul 23, 26 11:23

555fN5gc2Q2')) OR 855=(SELECT 855 FROM PG_SLEEP(15))--

EFSArCBy | Jul 23, 26 11:23

555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)

EFSArCBy | Jul 23, 26 11:23

555'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555'"

EFSArCBy | Jul 23, 26 11:23

@@ReCrt

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

-1 OR 2+432-432-1=0+0+0+1 -- | Jul 23, 26 11:23

555

-1 OR 2+569-569-1=0+0+0+1 | Jul 23, 26 11:23

555

-1' OR 2+859-859-1=0+0+0+1 -- | Jul 23, 26 11:23

555

-1' OR 2+408-408-1=0+0+0+1 or '4f5REbyK'=' | Jul 23, 26 11:23

555

-1" OR 2+146-146-1=0+0+0+1 -- | Jul 23, 26 11:23

555

if(now()=sysdate(),sleep(15),0) | Jul 23, 26 11:23

555

EFSArCBy0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z | Jul 23, 26 11:23

555

EFSArCBy0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z | Jul 23, 26 11:23

555

EFSArCBy-1 waitfor delay '0:0:15' -- | Jul 23, 26 11:23

555

EFSArCByouuZfiDs'; waitfor delay '0:0:15' -- | Jul 23, 26 11:23

555

EFSArCBydhMIfGUV' OR 925=(SELECT 925 FROM PG_SLEEP(15))-- | Jul 23, 26 11:23

555

EFSArCByOcoXxFbL') OR 369=(SELECT 369 FROM PG_SLEEP(15))-- | Jul 23, 26 11:23

555

EFSArCByU89UqlB3')) OR 704=(SELECT 704 FROM PG_SLEEP(15))-- | Jul 23, 26 11:23

555

EFSArCBy'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||' | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy'" | Jul 23, 26 11:23

555

@@BPTsh | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:23

555

EFSArCBy | Jul 23, 26 11:24

555

EFSArCBy | Jul 23, 26 11:24

555

EFSArCBy | Jul 23, 26 11:24

555

Add comment

Info

Please log in to comment!

Newsletter

Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.