nicolaswecandoit nicolaswecandoit - 3 years ago 246
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.

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download