views:

326

answers:

3

Hi,

I let users embed videos from Youtube, Google, Vimeo etc. I thought about the best and most secure approach (I don't want them to be able to include any flash and I also want to restrict the Videosites to exclude free porn websites etc.).

So I thought the best and easiest thing would be to let the user just copy&paste the URL of the video into a text-field, store it in a ExternalVideo Model and then just generate the needed HTML to embed the video.

So my ExternalVideo Model has a function called "embed_html" which should return the proper HTML.

Of course I could do something like this:

def embed_html
  # just a very short example to make my point
  "<embed src='#{@video_source}'>" 
end

But I think that's bad practice and very unreadable.

My Question: Is there a tool / Gem / Built-in function I can use to generate custom HTML, something like the View Helpers (link_to, image_tag, etc)?

Thanks for your help!

+1  A: 

You almost answered your question. Use helper method:

def embed_html url
  "<embed src='#{url}'>" 
end

And use it in view:

<%= embed_html @video_source %>
klew
that's not what I asked. I'm looking for a way to generate more complex html code. I didn't want to write the full example in my question, but if you check out the embed code of video sites its much more than the one-liner I posted as an example
ole_berlin
You can with helper method write as complicated html as you want. So I probably don't understand your question and by looking on other answers, they don't understand it too. Maybe give better explanation what you want.
klew
ole_berlin
A: 

Why don't you just write a view helper method that accepts a ExternalVideo model instance, asks it for its video URL and then returns the HTML?

John Topley
good idea, but my question is: How to return the html without using a very long, ugly string which isn't very readable? This question also applies to the use of a helper method
ole_berlin
+1  A: 

I would do the following

def embed_element(external_video)
  content_tag(:embed, '', :src => external_video.video_source)
end

You should probably check the docs for more information on the content tag method.

Also note that the content_tag() method will insert a closing tag. Something you seem to be forgetting...

Jongsma
thanks, that's what I was looking for. My example ist just to make clear what I want, of course my embed-code will be far more complex and with closing tags...
ole_berlin
You might also want to look into 'markaby' if you want to get much more complex.
Jongsma