Embed Youtube video in Rails app
Hello.
How is possible to embed an existing video an show it in a page with rails?
I followed this guide with yt gem:
https://www.sitepoint.com/youtube-api-version-3-rails/
So now I can post a youtube link and save it in the databse...
but doesn't explain how to embed the video and show it directly in the app...
Thank you very much!
The way I embed youtube videos on GoRails is that I have an Episode model with a column for youtube_id and when you click on an episode I embed it like the following (no need to use their API which is great):
<iframe src='https://www.youtube.com/embed/<%= @episode.youtube_id %>?rel=0&autoplay=<%= params[:autoplay] || 0 %>' frameborder='0' allowfullscreen></iframe>
@Zanger02, right where it says <%= @episode.youtube_id %>
in
<iframe src='https://www.youtube.com/embed/<%= @episode.youtube_id %>?rel=0&autoplay=<%= params[:autoplay] || 0 %>' frameborder='0' allowfullscreen></iframe>
Which would render something like:
<iframe class="embed-responsive-item" src="https://www.youtube.com/embed/lQOLrycmXC4?enablejsapi=1&autoplay=1" frameborder="0" allowfullscreen=""></iframe>
This assumes you're passing an @episode
object that has an attribute named youtube_id
- your specific app may vary.
Did someone try using video_tag or video.js? I working on a product that needs to display different video sources and having to embed different iframes/players depending on the source is not quite an option.
I second that Chris' answer should suffice. To answer Stephane, I usually have my model with a second column with the source, like YouTube or Vimeo. I create some logic that I can reuse on the view end where it reads which video source to use, then includes the appropriate code to load the video.