user841747
user841747

Reputation:

Render a youtube embed code in rails template

I am trying to embed a youtube embed in a rails view

In the controller I have

@embed_code = '<iframe width="480" height="270" src="https://www.youtube.com/embed/#{video_id}?frameborder="0"allowfullscreen"></iframe>'

I am failing to find a way to write this to the view effective escaping and making html safe and display it as <%= @embed_code.html_safe %>

If anyone can offer some advice I would appreciate it I have walked myself in circles and am well confused at this stage. As far as I see it is the second quotes around the params causing the issue.

Upvotes: 0

Views: 1799

Answers (3)

big-circle
big-circle

Reputation: 547

I think sanitize maybe what you want

%= sanitize @article.body %>


def sanitize(html, options = {})
  self.class.white_list_sanitizer.sanitize(html, options).try(:html_safe)
end

Upvotes: 0

Valery Kvon
Valery Kvon

Reputation: 4496

Controller:

@embed_code = %Q{<iframe.....#{video_id}..>...</iframe>}.html_safe
# or
@embed_code = ActiveSupport::SafeBuffer.new(%Q{<iframe.....#{video_id}..>...</iframe>})

View:

<%= @embed_code %>

Upvotes: 2

Baldrick
Baldrick

Reputation: 24340

Use

<%= raw @embed_code %>

The raw helper prevents Rails to automatically escape the html code in the view.

Upvotes: 0

Related Questions