西纳特拉 (Sinatra) 标题

发布于 2024-08-29 03:18:56 字数 965 浏览 4 评论 0原文

我正在尝试在 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

到底是什么出了问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

反话 2024-09-05 03:18:56

“究竟出了什么问题?”

嗯,错误消息是不言自明的。您调用此方法时,标题变量设置为nil。要重现错误,请尝试调用:

slugify(nil)

我想它在这一行失败了:

title = title.gsub(s, rep)

为了避免这种情况,您可以在方法的开头检查 nil 标题:

raise "wrong input!" if title.nil?

或者(恕我直言,更优雅)始终转换标题到字符串,最终将 nil 转换为空字符串:

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

顺便说一句,您的代码可能需要进行一些认真的重构。首先,尝试替换:

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

或使用

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

tr 方法而不是gsub 用于字符到字符的替换:

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

"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"
§普罗旺斯的薰衣草 2024-09-05 03:18:56

我无法复制你所看到的。这是我的测试(在 Sinatra 之外,所以只使用一个模块):

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

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.

断爱 2024-09-05 03:18:56

看看 iconv 的音译能力。它可以让您避免构建自己的字符转换表。

奥比·费尔南德斯来救援:http://www.jroller.com/obie/tags/unicode< /a>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文