【发布时间】:2020-11-22 03:43:40
【问题描述】:
所以,我有一个 Rails 应用程序,它允许用户提交 youtube 链接并使用竖起大拇指 gem 对歌曲/链接进行排名。
但是,用户必须将链接复制并粘贴到浏览器中才能收听歌曲。为了获得更好的用户体验,我希望将提交的 YouTube 链接作为视频嵌入到应用程序中。那么,如何动态地做到这一点呢?
换句话说,您将能够提交一个 YouTube 链接,它将被保存在例如歌曲 ID(1)。您将能够单击 song_id(1),而不仅仅是看到链接,您还将看到嵌入的 YouTube 视频。
更新 show.html.erb:
<%- model_class = Song -%>
<div class="page-header">
<h1><%=t '.title', :default => model_class.model_name.human.titleize %></h1>
</div>
<dl class="dl-horizontal">
<dt><strong><%= model_class.human_attribute_name(:title) %>:</strong></dt>
<dd><%= @song.title %></dd>
<dt><strong><%= model_class.human_attribute_name(:url) %>:</strong></dt>
<dd><%= YouTubeAddy.extract_video_id(@song.url) %> </dd>
<iframe width="#{width}" height="#{height}" src="http://www.youtube.com/embed/#{youtube_id}" frameborder="0"></iframe>
songhelper.rb
module SongsHelper
def youtube_id
YouTubeAddy.extract_video_id(@song.url)
end
end
【问题讨论】:
-
或许你可以试试this gem
-
我已经用我的立场更新了原帖,我做错了什么? :)
-
使用
<%= @foo %>表示法而不是#{ @foo }
标签: ruby-on-rails ruby-on-rails-4