2

ページネーションは簡単にできますが、少し複雑な 2 つのモデルを使用しています。

まずはコントローラーです

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>

私はそのビューをページネーションしようとしています。投稿だけではなく、投稿のハッシュタグです。正しい<%= will_paginate %>コードは何でしょうか?

投稿自体だけでなく、投稿のハッシュタグをページ分割しようとしています。

4

1 に答える 1

3

素晴らしい will_paginate gem: https://github.com/mislav/will_paginateを使用している場合は、コントローラーのコードを次のように単純に変更します。

# 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 %>
于 2013-05-08T10:42:47.307 に答える