今晚我遇到了一个非常烦人的问题,也许这里有人可以帮助我。
我正在用 nanoc 构建一个静态博客,目前正在为下一篇/上一篇文章制作一些助手(我包括了一些我所做的测试和返回):
# lib/helpers.rb
include Nanoc3::Helpers::Blogging
include Nanoc3::Helpers::LinkTo
# Returns a link to the next article
# If the article in param is the most recent, returns nothing
def next_article article
articles = sorted_articles # returns an array of article ordered by date
pos = articles.index(article) # returns the expected integer
pos.class # returns Fixnum
pos.nil? # returns false
pos + 1 # NoMethodError: undefined method `+' for nil:NilClass
return if pos.zero? # NoMethodError: undefined method `zero?' for nil:NilClass
link_to(articles[pos+1][:title], articles[pos+1]) # Fails too, obviously
end
我完全不知道为什么我不能使用“pos”变量,但仍然可以对其执行一些读取。如果有人有见解,我会接受。提前致谢 !
(我在 OSX Lion 上使用 ruby-1.9.3p194 和 rvm,如果它可能有任何关系)
更新:我应该准确地说 pos 的返回值是刚读取时的预期值。奇怪的是,设置
pos = articles.index(article).to_s.to_i
似乎工作。我只是不明白它是如何发生的以及为什么会发生。
#layout/post_navigation.haml
.post_navigation
.next
=next_article @item