Mel
Mel

Reputation: 2685

Embedding Youtube Video in Rails App

I am trying to add a you tube clip to my rails app.

I have a profiles helper that says

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

In my view i have

  <div class="embed-container"> 
        <%= embed(@profile.youtube_url) %>  
      </div>

I get an error with my helper that says:

NoMethodError at /profiles/8
undefined method `split' for nil:NilClass

Upvotes: 0

Views: 1074

Answers (1)

coderhs
coderhs

Reputation: 4837

from your error code its clear that the Profile with id 8, doesn't have a youtube url. So when you try to apply split (a method meant for string) on it, it causes an error.

Check if the user has a value for the field youtube_url.

To avoid cases in which the user doesn't have youtube_url i suggest the following change to your view.

<div class="embed-container">
  <%= embed(@profile.youtube_url) if @profile.youtube_url %>
</div>

Upvotes: 2

Related Questions