我正在尝试在 Sinatra 中创建一个 slug 助手。这是代码(如此处所示):
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
我不断收到此错误:
private method `gsub' called for nil:NilClass
到底出了什么问题?