在Rails中嵌入vimeo播放器?
我想允许用户提交vimeourl并将其嵌入播放器中。 是否有插件或gem这样做?
谢谢
Vimeo详细介绍了如何使用他们的API嵌入: http : //www.vimeo.com/api/docs/oembed
在他们的“非官方下载”页面下,有这个gem: http : //github.com/matthooks/vimeo
老问题,但我发现自己处于相同的情况。
如果您不想为video检索Vimeo iframe,可以使用以下代码。 无需添加gem。
添加助手
只需创建一个文件app/helpers/videos_helper.rb
和以下行。
module VideosHelper require 'net/http' VIMEO_REGEX = %r(^https?:\/\/(?:.*?)\.?(vimeo)\.com\/(\d+).*$) # Finds Vimeo's video ID from given URL or [nil] if URL is invalid def find_vimeo_id url url = sanitize url matches = VIMEO_REGEX.match url.to_str matches[2] if matches end # Get Vimeo video iframe from given URL def get_vimeo_iframe url, width, height vimeo_id = find_vimeo_id url uri = "https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/#{ vimeo_id }&width=#{ width }&height=#{ height }" response = Net::HTTP.get( URI.parse( uri )) json = JSON.parse response json['html'].html_safe end end
在视图中获取iframe
在您的视图中,只需调用get_vimeo_iframe()
即可打印iframe。
<%= get_vimeo_iframe('http://your.vimeo.url') %>
或者如果你想要,你可以添加宽度和高度
<%= get_vimeo_iframe('http://your.vimeo.url', '800px', '450px') %>
如果你想我创建这个要点 ,你也可以要求YouTube iframe(不仅仅是Vimeo)。