Rails3中的will_paginate哈希对象

3

看起来will_paginate不支持对hash对象进行分页。 我有一堆文章,基本上是一个yaml文件集合,想要在页面上输出。

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

有谁能给我建议,如何为这个写分页功能? 谢谢。

1个回答

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] %>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接