Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Audio assets in Rails brings No route matches

I'm trying to use an audio file in rails. I created a folder audios under app\assets\. I would like to use the assets precompile so that I don't have to put the file under app\public

Right now I'm getting

ActionController::RoutingError (No route matches [GET] "/audios/audio_file.wav")

If I change the url from URL/audios/audio_file.wav to URL/assets/audio_file.wav it works. How can I fix the problem? What is the right way?

like image 654
crazybob Avatar asked Aug 31 '13 19:08

crazybob


1 Answers

First, in case you didn't realize it already: your new app/assets/audios folder is already in the load path... you just need to restart your server for Sprockets to pick it up.

In development, assets are available at the relative url: /assets/<asset file name>.

For example, assuming your wav file is located at /app/assets/audios/audio_file.wav in the filesystem, it would be accessible at the relative url /assets/audio_file.wav in the browser. This is because Sprockets/Dev-Rails knows to search the /app/assets folder and its subdirectories when locating assets.

In production, assets precompilation (typically) happens on deploy. At this time, your wav file is copied to e.g. /public/assets/audio_file-<MD5 fingerprint>.wav and is accessible at the relative url: /assets/audio_file-<MD5 fingerprint>.wav.

Because of the different naming styles used between development and production, any time you want to refer to an asset you should do so using a helper method (even in CSS!). That is, production includes the MD5 fingerprint, whereas development does not. But you don't have to worry about any of that so long as you use a helper:

  • For images: <%= image_tag('homes/logo.png') %> -- given an image file that lives in /app/assets/images/homes/logo.png on the file system.
  • For non-standard assets, such as audio files: <%= asset_path('audio_file.wav') %>, which would produce a relative path of /assets/audio_file.wav.
like image 92
pdobb Avatar answered Oct 22 '22 12:10

pdobb