轨道 3 +延迟加载+ Kaminari 用于分页 +哈姆雷
我想使用 kaminari gem 对搜索结果进行分页。由于 gem 不会“污染”ActiveRecord::Base 和 Array,kaminari 实际上是我的首选方式(从架构的角度来看)。不幸的是,Rails 3 及其延迟加载概念让事情变得有点混乱(至少在我的场景中)。
在我的 HAML 模板中,我想列出所有搜索结果。当结果集为空时,我想显示一条消息,告诉用户没有找到结果。过去我可以执行以下操作:
- unless @results.empty?
... search results ...
- else
... no results found ...
= paginate @results
Since .empty?不强制加载 这显然在 Rails 3 中不再起作用。天哪,我可以在查询中使用 .all 来强制加载。不幸的是 .all 返回一个数组,并且 kaminari 的分页视图助手不适用于数组(恕我直言,这是正确的哲学)。我很好奇你会如何处理这种情况。有什么最佳实践吗?我所有的尝试都以丑陋的结构结束(如下所示)。 :/
为了让事情更清楚一点:
我的控制器操作:
helper_method :keys
def groups
@results = []
if params[:query].present?
@results = Group.public
.where({:body.matches_any => keys} | {:subject.matches_any => keys})
.order('groups.updated_at DESC')
.page(params[:page])
.per(1)
end
respond_to do |format|
format.html
end
end
我的“丑陋”HAML 模板:
%h1= "Search results for \"#{params[:query]}\""
%br
%ol
- @results.each do |group|
%li
= link_to group.subject, group
%br
= group.body
%br
= group.created_at
- if @results.empty?
%div No results found ...
- else
=paginate(@results)
这里的问题:它创建了一个 ol 标签,尽管没有找到结果。
有什么想法吗?
提前致谢!
I would like to use the kaminari gem to paginate over search results. Since the gem does not "pollute" ActiveRecord::Base and Array, kaminari is actually my preferred way to go (from an architectural point of view). Unfortunately there is Rails 3 and its lazy loading concept that makes things a little bit confusing (at least at my scenario).
In my HAML-Template I would like to list all of my search results. When the result set is empty, I want to display a message telling the user that no results have been found. In the past I could do the following:
- unless @results.empty?
... search results ...
- else
... no results found ...
= paginate @results
Since .empty? does not force loading this does obviously no longer work in Rails 3. Jep, I could use .all in my Query to force loading. Unfortunately .all returns an Array and kaminari's paginate View helper does not work with Arrays (the right philosophy imho). I am curious how you would handle this kind of situation. Any best practices? All of my attempts ended in ugly constructions (like the one below). :/
To make things a little bit clearer:
My Controller action:
helper_method :keys
def groups
@results = []
if params[:query].present?
@results = Group.public
.where({:body.matches_any => keys} | {:subject.matches_any => keys})
.order('groups.updated_at DESC')
.page(params[:page])
.per(1)
end
respond_to do |format|
format.html
end
end
My "ugly" HAML-Template:
%h1= "Search results for \"#{params[:query]}\""
%br
%ol
- @results.each do |group|
%li
= link_to group.subject, group
%br
= group.body
%br
= group.created_at
- if @results.empty?
%div No results found ...
- else
=paginate(@results)
The problem here: it creates an ol-tag although no results have been found.
Any Ideas?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我有用:
与 kaminari (0.12.3)
works for me:
with kaminari (0.12.3)