nicolaswecandoit nicolaswecandoit - 5 months ago 63
Ruby Question

Split youtube url rails

I m trying to parse youtube url. At this time my code is ok with standard youtube url.

But don't work with list or like this

My code is

module CampingsHelper
def embed(youtube_url)
youtube_id = youtube_url.split("=").last
content_tag(:iframe, nil, src: "//{youtube_id}")

How I can fix this ?

By the way I want to show an alert if url isn't from youtube.


You can use regular expression:

youtube_id = youtube_url[/(?<=v=)[^&]+|(?<=https:\/\/\/)[^&?]+/]

To show an alert can just check that URL doesn't start with, or