在 Rails 中嵌入 YouTube 视频

发布于 2024-12-18 05:01:21 字数 1620 浏览 0 评论 0原文

我正在尝试将视频嵌入页面中。我尝试了两种不同的方法,但没有成功

show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <b>Name:</b>
  <%= @video.name %>
</p>

<p>
  <b>Url:</b>
  <%= @video.url %>
</p>

<p>
    <% #video_tag( @video.url , :size => "560x315", :controls => true, :autobuffer => true ) %>
    <%= youtube_video @video.url%>
</p>

<%= link_to 'Edit', edit_video_path(@video) %> |
<%= link_to 'Back', videos_path %>

该视频不显示,其下面的链接也不显示。

任何提示将不胜感激。谢谢

这是调试结果

视频中的 ActionView::MissingTemplate#show

显示中 /Users/atbyrd/Documents/sites/city/app/views/videos/show.html.erb 第 15 行提出的位置:

缺少部分共享/视频:{:handlers=>[:erb, :builder, :咖啡], :formats=>[:html], :locale=>[:en, :en]}。搜索位置:* “/Users/atbyrd/Documents/sites/city/app/views”* “/Users/atbyrd/.rvm/gems/ruby-1.9.2-p290/gems/devise-1.5.1/app/views”

提取的源代码(大约第 15 行):

12: 13:

14: <% #video_tag( @video.url , :size => "560x315", :控制=> true, :autobuffer =>正确)%> 15: <%= youtube_video @video.url%> 16:

17: 18: <%= link_to '编辑', edit_video_path(@video) %> |

Rails.root:/Users/atbyrd/Documents/sites/city 应用程序跟踪 | 框架跟踪 |完整跟踪

app/helpers/application_helper.rb:4:in youtube_video' 应用程序/视图/视频/show.html.erb:15:in_app_views_videos_show_html_erb__1532956397246631491_70281299458880' app/controllers/videos_controller.rb:18:在“显示”

I'm trying to embed video in pages. I tried two different ways but no luck

show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <b>Name:</b>
  <%= @video.name %>
</p>

<p>
  <b>Url:</b>
  <%= @video.url %>
</p>

<p>
    <% #video_tag( @video.url , :size => "560x315", :controls => true, :autobuffer => true ) %>
    <%= youtube_video @video.url%>
</p>

<%= link_to 'Edit', edit_video_path(@video) %> |
<%= link_to 'Back', videos_path %>

The video does not show neither does the links below it.

Any tips would be appreciated. Thank you

this is the debug result

ActionView::MissingTemplate in Videos#show

Showing
/Users/atbyrd/Documents/sites/city/app/views/videos/show.html.erb
where line #15 raised:

Missing partial shared/video with {:handlers=>[:erb, :builder,
:coffee], :formats=>[:html], :locale=>[:en, :en]}. Searched in: *
"/Users/atbyrd/Documents/sites/city/app/views" *
"/Users/atbyrd/.rvm/gems/ruby-1.9.2-p290/gems/devise-1.5.1/app/views"

Extracted source (around line #15):

12: 13:

14: <% #video_tag( @video.url , :size => "560x315",
:controls => true, :autobuffer => true ) %> 15: <%= youtube_video
@video.url%> 16:

17: 18: <%= link_to 'Edit',
edit_video_path(@video) %> |

Rails.root: /Users/atbyrd/Documents/sites/city Application Trace |
Framework Trace | Full Trace

app/helpers/application_helper.rb:4:in youtube_video'
app/views/videos/show.html.erb:15:in
_app_views_videos_show_html_erb__1532956397246631491_70281299458880'
app/controllers/videos_controller.rb:18:in `show'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

緦唸λ蓇 2024-12-25 05:01:21

您忘记关闭括号

<%= video_tag( @video.url %>

应该

<%= video_tag( @video.url ) %>

更新:

尝试使用video_path而不是 video_tag。

第二次更新:

这是我自己在自己的网站上执行此操作的方法:

1 - 创建一个名为 _video.html.erb 的部分(我实际上使用haml,但如果您愿意,erb 也可以)并将其放在像 views/shared 这样的文件夹中,或者将以下代码放入其中:

<iframe width="490" height="275" src="<%= url %>" frameborder="0" allowfullscreen></iframe>

2 将以下方法添加到 application_helper.rb

module ApplicationHelper
  # this method will embed the code from the partial
  def youtube_video(url)
    render :partial => 'shared/video', :locals => { :url => url }
  end 
end

3 - 现在只需在您的视图中调用它即可:

<%= youtube_video @video.url %>

这对我来说没问题

you have forgotten to close a bracket

<%= video_tag( @video.url %>

should be

<%= video_tag( @video.url ) %>

UPDATE:

Try using video_path instead of video_tag.

2nd UPDATE:

here's how I do this myself on my own site:

1 - create a partial called _video.html.erb (I actually use haml, but erb will do if you prefer it) and put it in a folder like views/shared or something ad put the following code in it:

<iframe width="490" height="275" src="<%= url %>" frameborder="0" allowfullscreen></iframe>

2 add the following method to application_helper.rb

module ApplicationHelper
  # this method will embed the code from the partial
  def youtube_video(url)
    render :partial => 'shared/video', :locals => { :url => url }
  end 
end

3 - now just call this in your views with:

<%= youtube_video @video.url %>

This works ok for me

仙女山的月亮 2024-12-25 05:01:21

我明白了,你忘了使用 erb 语法。尝试:

<iframe width="560" height="315" src= "<%= #{@video.url} %>" frameborder="0" allowfullscreen></iframe>

I got it, you forgot to use erb syntax. Try:

<iframe width="560" height="315" src= "<%= #{@video.url} %>" frameborder="0" allowfullscreen></iframe>
电影里的梦 2024-12-25 05:01:21

@miaout17是对的,你不能在HTML中使用字符串插值,所以你必须将它包装在Erb中(例如<%= %>)。

另外,您在下面的链接中缺少结尾“)”,这可能就是它们不起作用的原因。

@miaout17 is right, you can't use string interpolation in HTML, so you have to wrap it in Erb (e.g. <%= %>).

Also you're missing an ending ')' in your links below which is probably why they aren't working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文