【发布时间】:2015-10-09 10:48:42
【问题描述】:
我有一个简单的 Rails 应用程序,它有一个显示 59 个帖子的索引页面。
问题是,如果我搜索某些内容,Post.count 会继续向我显示索引页面上的原始帖子数 - 例如,如果我搜索名为“Quilon”的帖子,我得到的搜索结果仅显示1 个帖子,但 Post.count 仍然显示原始帖子编号为 59。
我该如何解决这个问题?
INDEX.HTML.ERB 代码
<%= Post.count %>
帖子控制器中的搜索功能
def index
@posts = Post.all
if params[:search]
@posts = @posts.search(params[:search]).order("created_at DESC")
end
if params[:zagat_status].present?
@posts = @posts.zagat_status(params[:zagat_status]).order("created_at DESC")
end
end
【问题讨论】:
-
试试
<%= @posts.count %> -
我认为你需要替换@posts.count
-
@posts.count 有效。谢谢!
标签: ruby-on-rails ruby