Html 在一个简单的 Rails 应用程序中使用 HTML5 视频标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8146478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:31:45  来源:igfitidea点击:

using HTML5 video tag in a simple rails app

ruby-on-railshtmlvideoroutingerb

提问by delta2006

inside index.html.erb there is the following code

在 index.html.erb 里面有下面的代码

<video width="320" height="240" controls="controls">
      <source src="truffle1.mp4"/>
 Your browser does not support the video tag.
</video>

I am not sure where to put my mp4 video file so I put it at several places.
Next I fire up the rails server and use Chrome to open the index page. I see the black video frame but it does not play. and when I try open video in a new window. I get No route matches [GET] "/admin/truffle1.mp4" (note admin is just the namespace for the controller).

我不确定把我的 mp4 视频文件放在哪里,所以我把它放在了几个地方。
接下来我启动 rails 服务器并使用 Chrome 打开索引页面。我看到黑色视频帧,但没有播放。当我尝试在新窗口中打开视频时。我得到 No route matching [GET] "/admin/truffle1.mp4" (注意 admin 只是控制器的命名空间)。



seems like this is a rails routing problem...

似乎这是一个rails路由问题......

回答by Pedro Cori

When you say src="truffle1.mp4"you're telling Rails to look for that file from the current route (you're probably on localhost:3000/admin if you're trying it on a local server, so it's looking in localhost:3000/admin/truffle1.mp4).

当您说src="truffle1.mp4"要告诉 Rails 从当前路由中查找该文件时(如果您在本地服务器上尝试,您可能在 localhost:3000/admin 上,因此它在 localhost:3000/admin/truffle1 中查找.mp4)。

You could try giving it the route from the home of your app like so: src="/assets/media/truffle1.mp4", and put the file in that directory (you'll probably have to create it).

您可以尝试为它提供从应用程序主页开始的路由,如下所示:src="/assets/media/truffle1.mp4",并将文件放在该目录中(您可能必须创建它)。

EDIT

编辑

Following the answer provided by @Pragnesh Vaghela, I managed to make it work. Your first intuition was right. You're missing routing if you want to have your videos in /assets/videos. When you say:

按照@Pragnesh Vaghela 提供的答案,我设法使它起作用。你的第一直觉是对的。如果您想将视频放在 /assets/videos 中,则您缺少路由。当你说:

<%= video_tag "truffle1.mp4", :size => "320x240", :controls => true %>

the server will look for the file in all the assets directories that have been routed (by default: stylesheets, images, and javascripts). If you put your video in images, it should work, for example. If you want to have the /assets/videos directory searched also, you have to add the following line to your config/application.rb file:

服务器将在所有已路由的资产目录中查找该文件(默认情况下:样式表、图像和 javascripts)。例如,如果您将视频放入图像中,它应该可以工作。如果您还想搜索 /assets/videos 目录,则必须将以下行添加到您的 config/application.rb 文件中:

config.assets.paths << "#{Rails.root}/app/assets/videos"

You can put it under the line that says:

你可以把它放在下面的行下:

config.assets.enabled = true

I believe.

我相信。

Hope this works.

希望这有效。

回答by Pragnesh Vaghela

You can use the video_tag helper which builds an HTML 5 tag. Video files are loaded from 'public/videos' by default.

您可以使用 video_tag 助手来构建 HTML 5 标签。默认情况下,视频文件从“公共/视频”加载。

<%= video_tag "truffle1.mp4", :size => "320x240", :controls => true %>