Skip to content

Instantly share code, notes, and snippets.

@richardsondx
Created April 2, 2012 04:08
Show Gist options
  • Save richardsondx/2280636 to your computer and use it in GitHub Desktop.
Save richardsondx/2280636 to your computer and use it in GitHub Desktop.
the %Q{..} doesn't show the iframe. The html dom is being displayed on the view.
def youtube_embed(youtube_url)
if youtube_url[/youtu\.be\/([^\?]*)/]
youtube_id = $1
else
# Regex from # http://stackoverflow.com/questions/3452546/javascript-regex-how-to-get-youtube-video-id-from-url/4811367#4811367
youtube_url[/^.*((v\/)|(embed\/)|(watch\?))\??v?=?([^\&\?]*).*/]
youtube_id = $5
end
%Q{<iframe title="YouTube video player" width="640" height="390" src="http://www.youtube.com/embed/#{ youtube_id }" frameborder="0" allowfullscreen></iframe>}
end
<p>
<b>Name:</b>
<%= @post.name %>
</p>
<p>
<b>Link:</b>
<%= @post.link %>
</p>
<%= youtube_embed(@post.link) %> <br/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment