Named Routes Can Be Used for Static Content Too

Sometimes you have content under /public that might need to be referenced directly instead of through a controller. Or you may need to expose an asset using an absolute path (let’s say you’re using an external service that points to these assets). The last thing you want to be doing is embedding absolute paths in your code.

Just add this mapping in routes.rb:

map.audio '/audio/:item', :controller => nil

And now you get Rails’ automatic path helpers:

audio_path('greeting.mp3') # "/audio/greeting.mp3"
audio_url('greeting.mp3') # "http://www.example.com/audio/greeting.mp3"

About this entry