Question

I'm using Kaminari to paginate my posts page. A post has many tags and each tag links to a "Show" page that displays all posts with that tag. I'm trying to paginate this tags page but it never quite works.

tags/show

<div class="post-index">
    <h1><%= @tag.name %></h1>
    <ul>
      <% @tag.posts.each do |post| %>
        <li>
            <div class="post">
                <div class="featured-image">
                    <%= link_to image_tag(post.featured_image.url(:featured)), post_path(post) %>
                </div>

                <div class="info">
                    <div class="title">
                        <h2><%= link_to post.title, post_path(post) %></h2>
                    </div>

                    <div class="excerpt">
                        <p><%= truncate(strip_tags(post.body), length: 360) %></p>
                    </div>
                </div>
              </div>
            </li>
      <% end %>
    </ul>
    <%= paginate @tags %>
</div>

tag.rb

lass Tag < ActiveRecord::Base
    has_many :taggings
    has_many :posts, through: :taggings

    paginates_per 2
end

tags_controller.rb

class TagsController < ApplicationController
    def show
    @tag = Tag.find(params[:id]).page(params[:page])
    end
end

I've tried all I can think of. The examples Kaminari uses are

tag = Tag.order(:name).page(params[:page])

but this doesn't work and returns a no method "posts" error. I tried paginating the posts model instead but that doesn't work.

If I remove any pagination reference, it displays all the posts correctly on the tag page.

Thanks for any advice

Était-ce utile?

La solution

I'm not sure about what do you want to paginate? Tags or posts? In your code, you want to paginate "@tags", but you never define "@tags".

@tag = Tag.find(params[:id]) 
@posts = @tag.posts.page(params[:page])
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top