如何将 youtube 框架添加到 ERB 文件
问题描述:
我在 post.url 中有视频的 url
I have url of video in post.url
如何添加 YouTube 框架?
How can I add youtube frame?
我用过这个
<% @posts.each do |post| %>
<iframe width="560" height="315" src= <%= \" post.url \"%>
frameborder="0" allowfullscreen>
</iframe>
<% end %>
并得到这个错误
语法错误,意外的 $undefined ...reeze;
@output_buffer.append=( \" post.url \");
syntax error, unexpected $undefined ...reeze;
@output_buffer.append=( \" post.url \");
我也用过
src= <%= post.url %>
我什么都没看到
答
我不喜欢将 ERB 标签与 HTML 标签混合使用,因此我建议使用 content_tag
辅助方法:
I prefer not to mix ERB tags with HTML tags, therefore I would suggest using the content_tag
helper method instead:
<% @posts.each do |post| %>
<%= content_tag(:iframe, '', src: post.url,
width: 560, height: 315, frameborder: 0) %>
<% end %>
或者更好:定义一个辅助方法,例如helpers/application_helper.rb
:
Or even better: define a helper method in e.g. helpers/application_helper.rb
:
def youtube_frame(url)
content_tag(:iframe, '', src: url, width: 560, height: 315, frameborder: 0)
end
并在您的视图中使用该方法使代码更具可读性和更易于理解:
and use that method in your view to make the code more readable and easier to understand:
<% @posts.each do |post| %>
<%= youtube_frame(post.url) %>
<% end %>