Brian Low
Brian Low

Reputation: 11811

Rails - get the current request's 'URI Pattern'

How do you get the "URI Pattern" (as shown in rake routes) for the current HTTP request?

/blogs/:blog_id/posts/:id

I'd like to capture performance stats for each route. Need to distinguish between GET /blogs and GET/blogs/5.

Upvotes: 2

Views: 452

Answers (1)

Martin Schmidt
Martin Schmidt

Reputation: 309

It's done by Rails.application.routes.router.recognize. You can use it in a method like this.

def this_requests_pattern(request)
  Rails.application.routes.router.recognize(request) do |route|
    return route.path.spec.to_s
  end
end

Upvotes: 3

Related Questions