Olivier Lachance
Olivier Lachance

Reputation: 25

How to display a video from youtube link saved in database in rails

I need some help. My project is to build an app to workout. I don't want to host any video, so in my form I just save youtube links in my database instead of the actual videos. I don't know how to display the video when it comes from the link in my database, though. Any idea ?! thanks !

   <div class="exercices">
    <% for exercice in @workout.exercices %>
    <div class="row">
        <div class="col-md-4">
            <video src="http://<%= exercice.link %>" ></video>
        </div>
        <div class="col-md-2"><%= exercice.number %></div>
        <div class="col-md-6"><%= exercice.name %></div>
    </div>
   <% end %>

Upvotes: 0

Views: 1125

Answers (2)

Malware Skiddie
Malware Skiddie

Reputation: 318

You can directly embed video like this to display your video from your link.

<iframe width="560" height="315" src="//www.youtube.com/embed/DokUjuZmpCE" frameborder="0" allowfullscreen></iframe>  

to make things simpler add a helper method e.g

def embed(youtube_url)
    youtube_id = youtube_url.split("=").last
    content_tag(:iframe, nil, src: "//www.youtube.com/embed/#{youtube_id}")
  end

Then in your view

<%= embed(exercice.link) %>

Add some CSS and you are good go.

Upvotes: 0

Alfie
Alfie

Reputation: 2784

You could use one of these to achieve that :

https://github.com/seanbehan/videojs_rails

https://github.com/tgezginis/video_player

Upvotes: 1

Related Questions