我对ruby on rails很陌生,我希望得到一些帮助。我已经能够弄清楚如何将youtube剪辑嵌入到我的show页面并拥有标题show
显示视图
#video_show
%h1= @youtube.title.html_safe
%p.username
Shared by
= @video.user.name
about
= time_ago_in_words(@video.created_at)
.clearfix
.video_image_show
= @youtube.html.html_safe
视频控制器def index
@videos = Video.all.order("created_at DESC").paginate(page: params[:page], per_page: 20)
@video.link = Video.find(params[:id])
@youtube = OEmbed::Providers::Youtube.get(@video.link)
end
def show
@infos = Info.where(video_id: @video)
@comments = Comment.where(video_id: @video)
@random_video = Video.where.not(id: @Video).order("RANDOM()").first
@youtube = OEmbed::Providers::Youtube.get(@video.link)
end
但是当它在我的索引页显示缩略图和标题时,它告诉我我有一个未定义的方法,如'title'。
索引视图- @videos.each do |video|
.Video
.Video_image
= image_tag @youtube.thumbnail_url.html_safe, video
.Video_content
.title
%h2= @youtube.title.html_safe, video
.data.clearfix
%p.username
Shared by
= video.user.name
我是否可以将视频id链接到索引页,这将允许Oembed显示@youtube = Oembed::Providers::Youtube.get(@video.link)缩略图和标题?
thanks so much advance
您不能在控制器的index方法中设置@youtube
,因为此时您有一个视频数组。你需要的是这样的:
在你的控制器
def index
@videos = Video.all.order("created_at DESC").paginate(page: params[:page], per_page: 20)
end
和HAML视图
- @videos.each do |video|
- youtube = OEmbed::Providers::Youtube.get(video.link)
.Video
.Video_image
= image_tag youtube.thumbnail_url.html_safe, video
.Video_content
.title
%h2= youtube.title.html_safe, video
.data.clearfix
%p.username
Shared by
= video.user.name