Ruby on rails 的未定义方法“总页数”,如何正确分页?
我可以很容易地分页,但我使用了两个模型,这使它有点复杂 首先是控制器Ruby on rails 的未定义方法“总页数”,如何正确分页?,ruby-on-rails,ruby,pagination,will-paginate,Ruby On Rails,Ruby,Pagination,Will Paginate,我可以很容易地分页,但我使用了两个模型,这使它有点复杂 首先是控制器 class BrowseController < ApplicationController def index @hashtags = Hashtag.find(:all, :order => 'created_at DESC') @posts = post.all end end 然后是browse\index.html.erb视图 <ul> <% @posts.eac
class BrowseController < ApplicationController
def index
@hashtags = Hashtag.find(:all, :order => 'created_at DESC')
@posts = post.all
end
end
然后是browse\index.html.erb视图
<ul>
<% @posts.each do |post| %>
<% if post.hashtags.present? %>
<li> <%= link_to post.hashtags.map{|h| "##{h.hashtags}"}.join(', '), post.user %> </li>
<% else %>
<% end %>
<% end %>
</ul>
我正试图用页码标出那个观点。这不仅仅是帖子,而是帖子的标签。正确的代码是什么
我试图对帖子的标签进行分页,而不仅仅是帖子本身 如果您使用的是awesome will_paginate gem:,只需将控制器代码更改为:
# per_page 30 posts, change :per_page to the number you´d like
@posts = Post.paginate(:page => params[:page], :per_page => 30)
并将您的视图更改为:
<%= will_paginate @posts %>
如果您正在使用awesome will_paginate gem:,只需将控制器代码更改为:
# per_page 30 posts, change :per_page to the number you´d like
@posts = Post.paginate(:page => params[:page], :per_page => 30)
并将您的视图更改为:
<%= will_paginate @posts %>
你不能对数组进行分页,不要调用@posts上的所有内容,否则你会将所有帖子加载到内存中,这与使用分页的全部原因背道而驰。我应该澄清一下,你可以对数组进行分页,但这不是默认行为,我使用kaminari和paginate_array方法,但will_paginate的工作方式不同:你不能对数组进行分页,不要调用@posts上的所有内容。否则,你会将所有帖子加载到内存中,哪种方式会违反使用Pagination的全部原因?我应该说明你可以对数组进行分页,但这不是默认行为,我将kaminari与paginate_数组方法结合使用,但paginate的工作方式会有所不同:不过,我并不只是对帖子进行分页。这是帖子的标签。我还可以使用你的代码吗?这并不能回答这个问题。不过,我并不是在给帖子分页。这是帖子的标签。我还能用你的密码吗?这不能回答问题。