Justin Ramos
Justin Ramos

Reputation: 116

Rails 3: get route pattern for current controller action

Is there a way to get the route pattern that was matched from the controller action? E.g. if I have the following route defined...

get '/foo/:id.:format' => 'foo#show'

... within the FooController#show action, is it possible to get a string containing the route pattern (in this example "/foo/:id.:format")?

Upvotes: 1

Views: 741

Answers (1)

Justin Ramos
Justin Ramos

Reputation: 116

I figured it out. It's a bit of a hack, but here's what I came up with. Just add this to ApplicationController:

def get_route_pattern
  Rails.application.routes.router.recognize(request) { |route| return route.path.spec.to_s }
end

Upvotes: 5

Related Questions