我正在为youtube视频制作资源丰富的路线。所以,一个人只需在表单中粘贴youtube embed链接。在控制器中,我有一套正常的资源操作:
class VideosController < ApplicationController
def index
@videos = Video.all
end
def new
@video = Video.new
end
def create
Video.create(params[:video])
redirect_to :action => :index
end
def destroy
Video.destroy(params[:id])
redirect_to :action => :index
end
end
在视图中我只是展示它:(在Haml中)
- @page_title = 'Video'
#videos
%ul
= list_of(@videos) do |video|
%h1= video.title
!= video.link
= link_to "Delete", video_path(video), :method => :delete
= link_to "Add new video", new_video_path
%p#top
= link_to 'Go to top ↑', '#'
对于不使用Haml的人,!=
会逃脱字符串。 video.link
拥有YouTube嵌入代码
问题在于,当我创建新视频时,以及将其重定向回索引页面时,不会显示新创建的视频(通常会显示其他视频)。只有在我刷新页面后,它才会正常显示。
我在网络检查器中看到src
中缺少iframe
属性(这就是为什么视频没有显示)。但是当我查看页面源代码时,那里的一切都很正常。所以,认为这可能是Javascript的错,我试着禁用它。但没有改变。
答案 0 :(得分:1)
我认为你不想使用haml来逃避它...我想你想打电话
video.link.html_safe
注意:如果用户粘贴链接,则非常不安全。
更新 ---如果您打开了javascript开发控制台,则会弹出以下错误:
**Refused to execute a JavaScript script. Source code of script found within request.**
检查this answer for why it's refusing to due XSS这是一种既安全又有效的方法。您将在文本字段中粘贴youtube ID:ibWYROwadYs
<强> index.erb 强>
<% if session[:youtube].present? %>
<iframe width="480" height="360" src="http://www.youtube.com/embed/<%=session[:youtube]%>" frameborder="0" allowfullscreen></iframe>
<% end %>
<%= form_tag load_path do %>
<%= text_field_tag :youtube_id %>
<%= submit_tag "Submit" %>
<% end %>
<%= link_to "Clear", clear_path, :method => :delete %>
<强> home_controller.rb 强>
class HomeController < ApplicationController
def index
end
def smth
session[:youtube] = params[:youtube_id]
redirect_to :action => :index
end
def clear
session.clear
redirect_to :action => :index
end
end