2010-11-25 47 views
3

看起来像will_paginate不支持分页散列对象。 我有一个基本上是我想要在页面上输出的yaml文件集合的文章列表。rails3 will_paginate散列对象

def index 
    @articles = Dir["#{Rails.root}/blog_articles/*.yml"].inject({}) do |h, file| 
    h["#{File.basename(file, '.yml')}"] = YAML::load(File.open(file)) 
    h 
    end 
end 

任何人都可以建议我如何为此编写分页吗? 谢谢。

回答

6

在你的控制器:

def index 
    @articles = Dir["#{Rails.root}/blog_articles/*.yml"].inject({}) do |h, file| 
    h["#{File.basename(file, '.yml')}"] = YAML::load(File.open(file)) 
    h 
    end 

    # paginate the keys of the hash instead of the hash itself 
    @article_keys = @articles.keys.paginate 
end  

在您的观点:

<% @article_keys.each do |k| %> 
    <%= @articles[k] %>