Sinatra Title Slugs

Posted by Ethan Turkeltaub on Stack Overflow See other posts from Stack Overflow or by Ethan Turkeltaub
Published on 2010-04-17T23:23:51Z Indexed on 2010/04/17 23:33 UTC
Read the original article Hit count: 327

Filed under:
|
|
|

I'm trying to create a slug helper in Sinatra. Here's the code (as seen here):

helpers do
  def sluggify(title)
  accents = { 
    ['á','à','â','ä','ã'] => 'a',
    ['Ã','Ä','Â','À'] => 'A',
    ['é','è','ê','ë'] => 'e',
    ['Ë','É','È','Ê'] => 'E',
    ['í','ì','î','ï'] => 'i',
    ['Î','Ì'] => 'I',
    ['ó','ò','ô','ö','õ'] => 'o',
    ['Õ','Ö','Ô','Ò','Ó'] => 'O',
    ['ú','ù','û','ü'] => 'u',
    ['Ú','Û','Ù','Ü'] => 'U',
    ['ç'] => 'c', ['Ç'] => 'C',
    ['ñ'] => 'n', ['Ñ'] => 'N'
  }

  accents.each do |ac,rep|
    ac.each do |s|
      title = title.gsub(s, rep)
    end
  end

  title = title.gsub(/[^a-zA-Z0-9 ]/,"")
  title = title.gsub(/[ ]+/," ")    
  title = title.gsub(/ /,"-")
  title = title.downcase

end

end

I keep getting this error:

private method `gsub' called for nil:NilClass

What exactly is going wrong?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about sinatra