Zsolt
Zsolt

Reputation: 253

Rails 4 will_paginate articles/post by tags

trying to paginate a list of articles (ideas in my case) which are on my tag show page. So I am listing all ideas that are tagged with "loremipsum". The problem is :per_page => 3 doesn't seem to take effect. All ideas show up (I have 4 for tag "loremipsum") withour error. Pagination links also show at page bottom (but page2 doesn't work).

tags_controller:

  def show
    @tag = Tag.find_by_name(params[:id])
    @ideas = @tag.ideas.all.paginate(:per_page => 3, :page => params[:page])
  end

In show.html.erb

  <% @tag.ideas.each do |idea| %>....<% end %>
  <%= will_paginate @ideas %>

On my idea list page pagination works just fine so no clue. Any help much appreciated. Thanks in advance!

Upvotes: 3

Views: 149

Answers (2)

februaryInk
februaryInk

Reputation: 725

I would suggest using @ideas.each in your view rather than @tag.ideas.each.

<% @ideas.each do |idea| %> ... <% end %>
<%= will_paginate @ideas %>

That is, I believe the problem you encountered was because you were not iterating over the same collection that you applied the pagination to. Though @ideas will paginate, @tag.ideas will not.

Upvotes: 1

Ahmad Al-kheat
Ahmad Al-kheat

Reputation: 1795

Replace this line

@ideas = @tag.ideas.all.paginate(:per_page => 3, :page => params[:page])

with

@ideas = @tag.ideas.paginate(:per_page => 3, :page => params[:page])

I believe you don't need that all.

Upvotes: 0

Related Questions