Rails 4 - will_paginate

Katie M picture Katie M · Jul 25, 2013 · Viewed 11k times · Source

This is my first attempt using will_paginate (I know! Where have I been??)

titles_controller.erb

  def index
    @titles = Title.active.sorted.paginate(:page => params[:page])
  end

index.html.erb

<% will_paginate @titles.each do |title| %>

Error:

undefined method `total_pages' for #<Enumerator:0x00000002bacaf0>

WTF am I doing wrong? Thanks in advance.

Answer

Lucas picture Lucas · Jul 25, 2013

Please read will paginate docs. You need to write:

<%= will_paginate @posts %>

There is no need for adding each.

So entire view would look like:

<% @titles.each do |title| %>
  <!-- do smth with title -->
<% end %>

<%= will_paginate @titles %>