寻找短期修复来限制记录显示数量而不使用 :limit => 10

发布于 2024-10-05 14:49:08 字数 942 浏览 0 评论 0原文

我想知道是否有一种方法可以完成我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鱼窥荷 2024-10-12 14:49:08

我会在控制器中完成所有这些操作。视图不应该执行这种逻辑。您说您不想只限制为 10 个,因为您想要全部并进行一些过滤。你可以:

@comments = Comment.all(:joins => :workout, :conditions => {:workouts =>{:user_id => current_user.id}}, :order => "created_at DESC", :limit => 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:

@comments = Comment.all(:joins => :workout, :conditions => {:workouts =>{:user_id => current_user.id}}, :order => "created_at DESC", :limit => 10)
阳光下慵懒的猫 2024-10-12 14:49:08
@comments[0..9]

@comments[10..-1]
@comments[0..9]

and

@comments[10..-1]
寄风 2024-10-12 14:49:08

will-paginate gem 是您正在寻找的 droid 吗?

将 gem 添加到环境.rb 并安装后,您将修改控制器,以便它调用 paginate 而不是 findall

@comments = Comment.paginate(:all, :order => "created_at DESC", :per_page => 10)

然后在您的视图中添加分页控件(前进一页、后退一页等的链接)

<%= will_paginate @comments %>

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 or all:

@comments = Comment.paginate(:all, :order => "created_at DESC", :per_page => 10)

And then in your view, add the pagination controls (links to advance a page, go back a page, &c.)

<%= will_paginate @comments %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文