gpt4 book ai didi

ruby-on-rails-3 - rails3 will_paginate 哈希对象

转载 作者:行者123 更新时间:2023-12-04 07:19:34 27 4
gpt4 key购买 nike

看起来 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

谁能建议我如何为此编写分页?谢谢。

最佳答案

在你的 Controller 中:

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

关于ruby-on-rails-3 - rails3 will_paginate 哈希对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4280660/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com