问题
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'
回答1:
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
回答2:
I got it, you forgot to use erb syntax. Try:
<iframe width="560" height="315" src= "<%= #{@video.url} %>" frameborder="0" allowfullscreen></iframe>
回答3:
@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.
来源:https://stackoverflow.com/questions/8272026/embed-youtube-video-in-rails