instance method current_page?

Ruby on Rails edge

Since edge

Signature

current_page?(options = nil, check_parameters: false, method: :get, **options_as_kwargs)

True if the current request URI was generated by the given options.

Examples

Let’s say we’re in the http://www.example.com/shop/checkout?order=desc&page=1 action.

current_page?(action: 'process')
# => false

current_page?(action: 'checkout')
# => true

current_page?(controller: 'library', action: 'checkout')
# => false

current_page?(controller: 'shop', action: 'checkout')
# => true

current_page?(controller: 'shop', action: 'checkout', order: 'asc')
# => false

current_page?(controller: 'shop', action: 'checkout', order: 'desc', page: '1')
# => true

current_page?(controller: 'shop', action: 'checkout', order: 'desc', page: '2')
# => false

current_page?('http://www.example.com/shop/checkout')
# => true

current_page?('http://www.example.com/shop/checkout', check_parameters: true)
# => false

current_page?('/shop/checkout')
# => true

current_page?('http://www.example.com/shop/checkout?order=desc&page=1')
# => true

Different actions may share the same URL path but have a different HTTP method. Let’s say we sent a POST to http://www.example.com/products and rendered a validation error.

current_page?(controller: 'product', action: 'index')
# => false

current_page?(controller: 'product', action: 'create')
# => false

current_page?(controller: 'product', action: 'create', method: :post)
# => true

current_page?(controller: 'product', action: 'index', method: [:get, :post])
# => true

The default method: :get matches GET and HEAD requests, but not other safe methods: after an HTTP QUERY request to http://www.example.com/search,

current_page?('/search')
# => false

current_page?('/search', method: :query)
# => true

We can also pass in the symbol arguments instead of strings.

Parameters

options opt = nil
check_parameters key = false
method key = :get
options_as_kwargs keyrest
Source
# File actionview/lib/action_view/helpers/navigation_helper.rb, line 94
      def current_page?(options = nil, check_parameters: false, method: :get, **options_as_kwargs)
        unless request
          raise "You cannot use helpers that need to determine the current " \
                "page unless your view context provides a Request object " \
                "in a #request method"
        end

        if options.is_a?(Hash)
          check_parameters = options.delete(:check_parameters) { check_parameters }
          method = options.delete(:method) { method }
        else
          options ||= options_as_kwargs
        end

        method_matches = case method
        when :get
          request.get? || request.head?
        when Array
          method.include?(request.method_symbol) || (method.include?(:get) && request.head?)
        else
          method == request.method_symbol
        end
        return false unless method_matches

        url_string = URI::RFC2396_PARSER.unescape(url_for(options)).force_encoding(Encoding::BINARY)

        # We ignore any extra parameters in the request_uri if the
        # submitted URL doesn't have any either. This lets the function
        # work with things like ?order=asc
        # the behavior can be disabled with check_parameters: true
        request_uri = url_string.index("?") || check_parameters ? request.fullpath : request.path
        request_uri = URI::RFC2396_PARSER.unescape(request_uri).force_encoding(Encoding::BINARY)

        if %r{^\w+://}.match?(url_string)
          request_uri = +"#{request.protocol}#{request.host_with_port}#{request_uri}"
        end

        remove_trailing_slash!(url_string)
        remove_trailing_slash!(request_uri)

        url_string == request_uri
      end

Defined in actionview/lib/action_view/helpers/navigation_helper.rb line 94 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionView::Helpers::NavigationHelper

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close