Iframe 在Phoenix EEX模板中嵌入Youtube视频

Iframe 在Phoenix EEX模板中嵌入Youtube视频,iframe,youtube,elixir,phoenix-framework,Iframe,Youtube,Elixir,Phoenix Framework,我编写了以下代码来显示项目: <%= for project <- @projects do %> <div class="col-sm-4"> <div class="panel panel-default" style="min-height: 1200px; margin-bottom:50px;height:auto;"> <div class="panel-heading"> <img src="&

我编写了以下代码来显示项目:

<%= for project <- @projects do %>
<div class="col-sm-4">
 <div class="panel panel-default"  style="min-height: 1200px; margin-bottom:50px;height:auto;">
    <div class="panel-heading">
        <img src="<%= Microflow.Avatar.url({project.picture, project}, :original) %>" alt="" width="330px" height="240px"/>

      </div>
    <div class="panel-body" style="min-height: 350px">
      <h2 style="font-family: 'Montserrat', sans-serif;"><%= project.name %></h2>
      <h3 style="font-family: 'Raleway', sans-serif;"><%= raw(project.description) %></h3>
      <h2 style="font-family: 'Montserrat', sans-serif;">

      </h2>
      <h2 style="font-family: 'Montserrat', sans-serif;"><%= project.raise_amount %> USD</h2>
    <iframe width="100%" height="100%" src= "<%=project.video_url%>"</iframe>  
<%= link "Delete Project", to: project_path(@conn, :delete, project), method: :delete, data: [confirm: "Are you sure?"], class: "btn btn-info btn-xs" %>
<%= link "Start Your Own Project", to: project_path(@conn, :new), method: :new, class: "btn btn-success btn-xs" %>
          </div>
           </div>
             </div>
<% end %>
我是否需要修复路由或定义函数?。。。以下Ruby指南可能仍适用于Phoenix/Elixir,但需要进行一些调整:


看起来您可能正在使用视频URL的相对路径。这意味着它将相对于您当前的URL提供服务

例如,如果您在
http://example.com/projects/1
项目.video\u url
/some\u video\u url
,那么iframe将使用`

如果您试图嵌入YouTube视频且仅具有id,则需要预先添加,这意味着iframe将使用:

解决了这个问题:

<% video_string = to_string(project.video_url) %>
 <% video = String.slice(video_string,32,String.length(video_string)) %>
 <iframe width="100%" height="100%" position= "absolute"  src="https://www.youtube.com/embed/<%=video%>"></iframe> 


我肯定会将该调用封装在函数中,这正是您提到@Gazler的原因。调用函数是实现DRY的一种很好的方法。谢谢Gazler,这在一定程度上有所帮助。我的原始代码适用于特定视频的绝对路径,但当我调用保存在数据库中的youtube URL时,它们不会显示。使用您编写的代码行将我链接到一个空白的youtube视频。我想我会用克里斯·麦考德在《凤凰城》一书中的例子来代替。也许不是最佳实践,但这是目前的解决办法。
"No route found for GET /Video%20Goes%20Here (MyApp.Router)"
<iframe width="100%" height="100%" src= "https://www.youtube.com/embed/<%=project.video_url%>"</iframe>
<% video_string = to_string(project.video_url) %>
 <% video = String.slice(video_string,32,String.length(video_string)) %>
 <iframe width="100%" height="100%" position= "absolute"  src="https://www.youtube.com/embed/<%=video%>"></iframe>