得到这个错误 syntax-6ren">
gpt4 book ai didi

ruby-on-rails - 如何将 youtube 框架添加到 ERB 文件

转载 作者:太空宇宙 更新时间:2023-11-03 17:22:29 25 4
gpt4 key购买 nike

我在 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 标签与 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

并在您的 View 中使用该方法使代码更具可读性和更易于理解:

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

关于ruby-on-rails - 如何将 youtube 框架添加到 ERB 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28979938/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com