current_page?(options) public

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

Examples

Let’s say we’re in the /shop/checkout?order=desc action.

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

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

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

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

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

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

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

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

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

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

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

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

  current_page?(:controller => 'library', :action => 'checkout')
  # => false
Show source
Register or log in to add new notes.
January 16, 2009
4 thanks

/products/1

Code example

  current_page?(product_path(@product))
  # => true
January 2, 2009
2 thanks

current_action? and current_controller?

I use them in link_unless_current_controller helper.

  def current_action?(options)
    url_string = CGI.escapeHTML(url_for(options))
    params = ActionController::Routing::Routes.recognize_path(url_string, :method => :get)
    params[:controller] == @controller.controller_name && params[:action] == @controller.action_name
  end

  def current_controller?(options)
    url_string = CGI.escapeHTML(url_for(options))
    params = ActionController::Routing::Routes.recognize_path(url_string, :method => :get)
    params[:controller] == @controller.controller_name
  end
December 11, 2008
2 thanks

with resources

/products

  current_page?(products_path)
  # => true