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?
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:
<%= image_tag('homes/logo.png') %>
-- given an image file that lives in /app/assets/images/homes/logo.png
on the file system.<%= asset_path('audio_file.wav') %>
, which would produce a relative path of /assets/audio_file.wav
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With