method

current_page?

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 action.

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

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

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

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

4Notes

/products/1

bansalakhil · Jan 16, 20097 thanks

==== Code example

current_page?(product_path(@product))
# => true

with resources

Finist · Dec 11, 20083 thanks

/products

==== current_page?(products_path) # => true

current_action? and current_controller?

qoobaa · Jan 2, 20093 thanks

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

active_link_to helper gem

tordans · Nov 4, 20111 thank

You should also have a look at https://github.com/twg/active_link_to if you need an 'active' class on your links.