rails3 will_paginate哈希对象

Pra*_*oya 3 will-paginate ruby-on-rails-3

看起来像will_paginate不支持对散列对象进行分页.我有一个文章列表,基本上是我想在页面上输出的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
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议我如何写这个分页?谢谢.

bow*_*ior 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    
Run Code Online (Sandbox Code Playgroud)

在您的观点中:

<% @article_keys.each do |k| %>
  <%= @articles[k] %>
Run Code Online (Sandbox Code Playgroud)