Method: WillPaginate::ViewHelpers#will_paginate

Defined in:
lib/will_paginate/view_helpers.rb

#will_paginate(collection = nil, options = {}) ⇒ Object

Renders Digg/Flickr-style pagination for a WillPaginate::Collection object. Nil is returned if there is only one page in total; no point in rendering the pagination in that case…

Options

Display options:

  • :previous_label – default: “« Previous” (this parameter is called :prev_label in versions 2.3.2 and older!)

  • :next_label – default: “Next »”

  • :page_links – when false, only previous/next links are rendered (default: true)

  • :inner_window – how many links are shown around the current page (default: 4)

  • :outer_window – how many links are around the first and the last page (default: 1)

  • :separator – string separator for page HTML elements (default: single space)

HTML options:

  • :class – CSS class name for the generated DIV (default: “pagination”)

  • :container – toggles rendering of the DIV container for pagination links, set to false only when you are rendering your own pagination markup (default: true)

  • :id – HTML ID for the container (default: nil). Pass true to have the ID automatically generated from the class name of objects in collection: for example, paginating ArticleComment models would yield an ID of “article_comments_pagination”.

Advanced options:

  • :param_name – parameter name for page number in URLs (default: :page)

  • :params – additional parameters when generating pagination links (eg. :controller => "foo", :action => nil)

  • :renderer – class name, class or instance of a link renderer (default: WillPaginate::LinkRenderer)

All options not recognized by will_paginate will become HTML attributes on the container element for pagination links (the DIV). For example:

<%= will_paginate @posts, :style => 'font-size: small' %>

… will result in:

<div class="pagination" style="font-size: small"> ... </div>

Using the helper without arguments

If the helper is called without passing in the collection object, it will try to read from the instance variable inferred by the controller name. For example, calling will_paginate while the current controller is PostsController will result in trying to read from the @posts variable. Example:

<%= will_paginate :id => true %>

… will result in @post collection getting paginated:

<div class="pagination" id="posts_pagination"> ... </div>


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/will_paginate/view_helpers.rb', line 89

def will_paginate(collection = nil, options = {})
  options, collection = collection, nil if collection.is_a? Hash
  unless collection or !controller
    collection_name = "@#{controller.controller_name}"
    collection = instance_variable_get(collection_name)
    raise ArgumentError, "The #{collection_name} variable appears to be empty. Did you " +
      "forget to pass the collection object for will_paginate?" unless collection
  end
  # early exit if there is nothing to render
  return nil unless WillPaginate::ViewHelpers.total_pages_for_collection(collection) > 1
  
  options = options.symbolize_keys.reverse_merge WillPaginate::ViewHelpers.pagination_options
  if options[:prev_label]
    WillPaginate::Deprecation::warn(":prev_label view parameter is now :previous_label; the old name has been deprecated", caller)
    options[:previous_label] = options.delete(:prev_label)
  end
  
  # get the renderer instance
  renderer = case options[:renderer]
  when String
    options[:renderer].to_s.constantize.new
  when Class
    options[:renderer].new
  else
    options[:renderer]
  end
  # render HTML for pagination
  renderer.prepare collection, options, self
  renderer.to_html
end