Rails: Best practice for creating custom HTML in a model?

I allow users to embed videos from Youtube, Google, Vimeo, etc. I was thinking of the best and safest approach (I do not want them to be able to include any flash memory, and I also want to limit the video to exclude free porn sites, etc.).

Therefore, I thought that it’s best and easiest to let the user simply copy and paste the video URL into a text box, save it in the ExternalVideo model and then simply generate the necessary HTML code for embedding the video.

So, my ExternalVideo model has a function called "embed_html" that should return the correct 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 is bad practice and very unreadable.

My question is: is there a / Gem / built-in function that I can use to create custom HTML, something like View Helpers (link_to, image_tag, etc.)?

Thank you for your help!

+3
source share
3 answers

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 inserts a closing tag. Something you seem to forget ...

+4
source

You almost answered your question. Use helper method:

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

And use it in sight:

<%= embed_html @video_source %>
+1
source

, ExternalVideo, URL- HTML?

0

Source: https://habr.com/ru/post/1711658/


All Articles