Rails中的音频资产没有路由匹配 [英] Audio assets in Rails brings No route matches
问题描述
我正在尝试在Rails中使用音频文件.我在 app \ assets \
下创建了一个文件夹 audios
.我想使用资产进行预编译,这样我就不必将文件放在 app \ public
现在我要得到
ActionController :: RoutingError(没有路由与[GET]"/audios/audio_file.wav"匹配)
如果我将网址从 URL/audios/audio_file.wav
更改为 URL/assets/audio_file.wav
,则它可以正常工作.我该如何解决该问题?正确的方法是什么?
首先,如果您尚未意识到它:新的 app/assets/audios
文件夹已经在加载路径中...您只需要重新启动服务器,Sprockets就可以收起它.
在开发中,资产位于相对网址:/assets/<资产文件名>
.
例如,假设您的wav文件位于文件系统中的/app/assets/audios/audio_file.wav
中,则可以通过相对URL /assets/audio_file进行访问.wav
在浏览器中.这是因为Sprockets/Dev-Rails知道在查找资产时搜索/app/assets
文件夹及其子目录的原因.
在生产中,资产预编译(通常)是在部署时进行的.此时,您的wav文件已复制到例如/public/assets/audio_file-< MD5指纹> .wav
,并且可以在相对URL上访问:/assets/audio_file-< MD5指纹> .wav
./p>
由于在开发和生产之间使用了不同的命名方式,因此,每当您要引用资产时,都应使用辅助方法(即使在CSS中也是如此).也就是说,生产中包含MD5指纹,而开发则不包含.但是,只要使用助手,您就不必担心任何事情:
- 对于图像:
<%= image_tag('homes/logo.png')%>
-给定一个位于/app/assets/images/homes中的图像文件/logo.png
在文件系统上. - 对于非标准资产,例如音频文件:
<%= asset_path('audio_file.wav')%>
,这会产生/assets/的相对路径audio_file.wav
.
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:
- 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
.
这篇关于Rails中的音频资产没有路由匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!