如何将 youtube 框架添加到 ERB 文件

How to add youtube frame to ERB file

我在 post.url

中有 url 个视频

如何添加 youtube 框架?

我用过这个

<% @posts.each do |post| %>

<iframe width="560" height="315" src= <%= \" post.url \"%>  

    frameborder="0" allowfullscreen>

</iframe> 

<% end %>

并得到这个错误

syntax error, unexpected $undefined ...reeze;
@output_buffer.append=( \" post.url \");

我也用过

 src= <%= post.url %>  

我什么也没看到

您错过了 erb 标签周围的引号:

src="<%= post.url %>"

因此您的代码将是:

<% @posts.each do |post| %>

<iframe width="560" height="315" src="<%= post.url %>" frameborder="0" allowfullscreen></iframe> 

<% end %>

我不想将 ERB 标签与 HTML 标签混合使用,因此我建议改用 content_tag 辅助方法:

<% @posts.each do |post| %>
  <%= content_tag(:iframe, '', src: post.url, 
                   width: 560, height: 315, frameborder: 0) %>
<% end %>

或者更好: 定义一个辅助方法,例如helpers/application_helper.rb:

def youtube_frame(url)
  content_tag(:iframe, '', src: url, width: 560, height: 315, frameborder: 0)     
end

并在您的视图中使用该方法使代码更易读和更容易理解:

<% @posts.each do |post| %>
  <%= youtube_frame(post.url) %>
<% end %>