0

im following this sitepoint moviestore tutorial and have everything working fine apart from a a video not loading in an iframe after a user has purchased the movie. I have a csv file with youtube urls in a column for testing the apps payment system (using braintree).

Looking at the source code of the html, the youtube link shows up without the embed part so its finding the info from the db file but its just not inputting the embed into the link. I did actually manage to get a video to work by manually inputting "/embed/" into the youtube link in the CSV.

Im fairly new to rails but if someone could explain why its not working for me id be very grateful.

this is what my movie.rb model looks like:

class Movie < ActiveRecord::Base
has_many :purchases
has_many :buyers, through: :purchases

before_save :embed_video_url

def poster
    "http://ia.media-imdb.com/images/M/#{poster_url}"
end

def imdb
    "http://www.imdb.com/title/#{imdb_id}/"
end

def embed_video_url
 self.video_url = "//www.youtube.com/embed/#{video_url.split('v=')[1].split('&list')[0]}"
end

def cart_action(current_user_id)
 if $redis.sismember "cart#{current_user_id}", id
   "Remove from"
else
  "Add to"
end
end     
end

The view contains the iframe

<%if signed_in?%>
<%if current_user.purchase? @movie %>
  <div class="flex-video">
    <iframe title="YouTube video player" width="100%" height="" src="<%= @movie.video_url %>" frameborder="0" allowfullscreen></iframe>
  </div>
<%else%>
  <%=link_to "", class: "button", data: {target: @cart_action, addUrl: add_to_cart_path(@movie), removeUrl: remove_from_cart_path(@movie)} do%>
    <i class="fi-shopping-cart"></i>
    <span><%=@cart_action%></span> Cart
  <%end%>
<%end%>

this is what the controller looks like for movies

class MoviesController < ApplicationController
 def index
  @movies = Movie.all
 end

 def show
  @movie = Movie.find(params[:id])
  @cart_action = @movie.cart_action current_user.try :id
 end
end

Thanks in advance!

MCrolio
  • 31
  • 4

2 Answers2

0

In the method embed_video_url you using video_url which should be self.video_url

Your method may not handle all the cases, so check Converting a regular Youtube 'link' into an embedded video question.

Or you can get the embeded_url automatically, then check out Video Info gem. It supports youtube, vimeo, Dailymotion and VK.

Community
  • 1
  • 1
mohameddiaa27
  • 3,587
  • 1
  • 16
  • 23
  • thanks for the reply. but if you look again you will see that i have in fact used `self.video_url` in `embed_video_url`. or are you referring to calling it somewhere else? – MCrolio Nov 04 '14 at 15:30
  • @MCrolio `video_url.split` is where you call it without `self` – mohameddiaa27 Nov 04 '14 at 15:33
  • im sorry but i dont follow. call it where? and what am i calling without self. as i look at the code i am doing exactly what you are referring to – MCrolio Nov 04 '14 at 23:40
  • self.video_url = "//www.youtube.com/embed/#{`video_url`.split('v=')[1].split........... – mohameddiaa27 Nov 05 '14 at 01:35
0

For anyone who has had a similar problem. Douglas F Shearer's solution from another SO post seemed to have fixed the issue. needed a little modification. This is most probably not the cleanest solution but see below...

Added this to a movie_helper.

def youtube_embed
  youtube_url = @movie.video_url
  if youtube_url[/youtu\.be\/([^\?]*)/]
    youtube_id = $1
  else
    youtube_url[/^.*((v\/)|(embed\/)|(watch\?))\??v?=?([^\&\?]*).*/]
    youtube_id = $5
  end

 %Q{<iframe title="YouTube video player" width="100%" height="390" src="http://www.youtube.com/embed/#{ youtube_id }" frameborder="0" allowfullscreen></iframe>}
end

I set youtube_url = @movie.video_url to retrieve the video_url from the CSV.

And then simply called it in movie view...

<p><%= raw(youtube_embed()) %></p>

I had to add raw() to prevent iframe being rendered as a string.

However it wont work when the code is in a model. The view doesn't recognise youtube_embed().

Community
  • 1
  • 1
MCrolio
  • 31
  • 4