I need to know the current route in a filter in rails.. how can I find out?
I'm doing REST resources, and no named routes
I need to know the current route in a filter in rails.. how can I find out?
I'm doing REST resources, and no named routes
I'll assume you mean the URI:
class BankController < ActionController::Base
before_filter :pre_process
def index
# do something
end
private
def pre_process
logger.debug("The URL" + request.url)
end
end
As per your comment below, if you need the name of the controller, you can simply do this:
private
def pre_process
self.controller_name # Will return "order"
self.controller_class_name # Will return "OrderController"
end
You can get most any data pertaining to the current path/route via the request object. This is accessible in your controller. You can read more about it here.
Take note of the "path_parameters" attribute, which returns a hash that contains the controller and the action that was requested.
To find out URI:
current_uri = request.env['PATH_INFO']
# If you are browsing http://example.com/my/test/path,
# then above line will yield current_uri as "/my/test/path"
To find out the route i.e. controller, action and params:
path = ActionController::Routing::Routes.recognize_path "/your/path/here/"
controller = path[:controller]
action = path[:action]
# You will most certainly know that params are available in 'params' hash