Rails - hash_for_named_url goodness

February 21, 2008

Update In Rails 4 these methods are gone. However, you can accomplish the same thing using Rails.application.routes.recognize_path(faq_path)

It’s well documented that if you use named routes in Rails you get the benefit of saying things like faq_url and faq_path in your code.

But what if you want the hash that makes up that named route? Why? Because there are two expire_page methods and the one that is available to cache sweepers expects a hash not a url. And it would be lovely to use my named routes and get back the hash.

I didn’t see this documented anywhere, but in the source alongside the aforementioned methods a named route will generate hash_for_faq_url and hash_for_faq_path.

Wahoo!