Module: Kaminari::Helpers::UrlHelper

Included in:
HelperMethods
Defined in:
lib/kaminari/helpers/helper_methods.rb

Overview

The Kaminari::Helpers::UrlHelper module provides useful methods for generating a path or url to a particular page. A class must implement the following methods:

* <tt>url_for</tt>: A method that generates an actual path
* <tt>params</tt>: A method that returns query string parameters
* <tt>request</tt>: A method that returns a Rack::Request object

A normal Rails controller implements all the methods, which make it trivial to use this module:

Examples

class UsersController < ApplicationController
  include Kaminari::Helpers::UrlHelper

  def index
    @users = User.page(1)

    path_to_next_page(@items)
    # => /items?page=2
  end
end

Instance Method Summary collapse

Instance Method Details

#next_page_path(scope, options = {}) ⇒ Object Also known as: path_to_next_page

A helper that calculates the path to the next page.

Examples

Basic usage:

<%= path_to_next_page @items %>
#-> /items?page=2

It will return ‘nil` if there is no next page.



76
77
78
# File 'lib/kaminari/helpers/helper_methods.rb', line 76

def next_page_path(scope, options = {})
  Kaminari::Helpers::NextPage.new(self, **options.reverse_merge(current_page: scope.current_page)).url if scope.next_page
end

#next_page_url(scope, options = {}) ⇒ Object Also known as: url_to_next_page

A helper that calculates the url to the next page.

Examples

Basic usage:

<%= next_page_url @items %>
#-> http://www.example.org/items?page=2

It will return ‘nil` if there is no next page.



41
42
43
# File 'lib/kaminari/helpers/helper_methods.rb', line 41

def next_page_url(scope, options = {})
  "#{request.base_url}#{next_page_path(scope, options)}" if scope.next_page
end

#path_to_next_url(scope, options = {}) ⇒ Object



46
47
48
49
# File 'lib/kaminari/helpers/helper_methods.rb', line 46

def path_to_next_url(scope, options = {})
  ActiveSupport::Deprecation.warn 'path_to_next_url is deprecated. Use next_page_url or url_to_next_page instead.'
  next_page_url(scope, options)
end

#prev_page_path(scope, options = {}) ⇒ Object Also known as: previous_page_path, path_to_previous_page, path_to_prev_page

A helper that calculates the path to the previous page.

Examples

Basic usage:

<%= path_to_prev_page @items %>
#-> /items

It will return ‘nil` if there is no previous page.



90
91
92
# File 'lib/kaminari/helpers/helper_methods.rb', line 90

def prev_page_path(scope, options = {})
  Kaminari::Helpers::PrevPage.new(self, **options.reverse_merge(current_page: scope.current_page)).url if scope.prev_page
end

#prev_page_url(scope, options = {}) ⇒ Object Also known as: previous_page_url, url_to_prev_page, url_to_previous_page

A helper that calculates the url to the previous page.

Examples

Basic usage:

<%= prev_page_url @items %>
#-> http://www.example.org/items

It will return ‘nil` if there is no previous page.



60
61
62
# File 'lib/kaminari/helpers/helper_methods.rb', line 60

def prev_page_url(scope, options = {})
  "#{request.base_url}#{prev_page_path(scope, options)}" if scope.prev_page
end