开发者

Sinatra Title Slugs

I'm trying to create a slug helper in Sinatra. Here's the code (as see开发者_如何学Cn 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?


"What exactly is going wrong?"

Well, the error message is kinda self-explanatory. You called this method with title variable set to nil. To reproduce the error, try calling:

slugify(nil)

I suppose that it failed on this line:

title = title.gsub(s, rep)

To avoid this, you can either check for nil title at the beginning of the method:

raise "wrong input!" if title.nil?

or (IMHO more elegant) always convert title to string, which converts eventual nil to empty string:

title = title.to_s # "any string" => "any string", nil => ""

BTW your code could use some serious refactoring. For a start, try replacing:

title = title.gsub(/a/,/b/)

with

title.gsub!(/a/,/b/)

or use tr method instead of gsub for character-to_character replacement:

"woot".tr("wt","WT") # => "WooT"


I'm not able to duplicate what you're seeing. Here's my test (outside of Sinatra, so just using a module):

module Helpers
  def self.sluggify(title)
    accents = {['á','à','â','ä','ã'] => 'a'} # Shortened

    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

puts Helpers.sluggify("Testing this!")

Yields testing-this.


Take a look at iconv's transliteration ability. It will let you avoid building your own table of character conversions.

Obie Fernandez to the rescue: http://www.jroller.com/obie/tags/unicode

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜