寻找短期修复来限制记录显示数量而不使用 :limit => 10
我想知道是否有一种方法可以完成我尝试使用 Ruby on Rails 应用程序完成的这个 hack。我在某些关联方面遇到了麻烦,但找不到解决方案。您可以 在这里查看这个问题。
同时,我想将记录的显示限制为 10
这样的数字,但我不想通过 :limit => 在控制器中执行此操作10
方式,因为我需要循环遍历所有记录。我什至不确定这是否可能,但我想我会问。
我的视图代码是:
<% @comments.each do |comment| %>
<% if comment.workout.user_id == current_user.id %><br/>
<%= link_to (comment.user.username), comment.user %> <br/>
<%= time_ago_in_words(comment.created_at) %> <br/>
<%= link_to (comment.workout.title), comment.workout %><br/>
<%= sanitize(simple_format(comment.body), :tags => %w(p)) %>
<% end %>
<% end %>
我的控制器只是调用
@comments = Comment.all(:order => "created_at DESC")
I am wondering if there is a way to complete this hack that I am trying to do with my Ruby on Rails app. I'm having trouble with some associations and I can't find the solution. You can see that question here.
In the mean time I am wanting to limit the display of records to a number like 10
but I don't want to do it in the controller through the :limit => 10
manner because I need to loop through all the records. I'm not even sure if this is possible but thought I would ask.
My view code is:
<% @comments.each do |comment| %>
<% if comment.workout.user_id == current_user.id %><br/>
<%= link_to (comment.user.username), comment.user %> <br/>
<%= time_ago_in_words(comment.created_at) %> <br/>
<%= link_to (comment.workout.title), comment.workout %><br/>
<%= sanitize(simple_format(comment.body), :tags => %w(p)) %>
<% end %>
<% end %>
My controller is simply calling
@comments = Comment.all(:order => "created_at DESC")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会在控制器中完成所有这些操作。视图不应该执行这种逻辑。您说您不想只限制为 10 个,因为您想要全部并进行一些过滤。你可以:
I would do all that in the controller. Views shouldn't be doing that logic. You say you don't want to only limit to 10, because you want all and do some filter. You can just:
和
and
will-paginate gem 是您正在寻找的 droid 吗?
将 gem 添加到环境.rb 并安装后,您将修改控制器,以便它调用 paginate 而不是
find
或all
:然后在您的视图中添加分页控件(前进一页、后退一页等的链接)
Is the will-paginate gem the droid you're looking for?
After adding the gem to your environment.rb and installing it, you would modify your controller so that it calls paginate rather than
find
orall
:And then in your view, add the pagination controls (links to advance a page, go back a page, &c.)