【发布时间】:2015-06-29 13:12:29
【问题描述】:
我正在尝试将你的 Tube 合并到我的 rails 4 应用程序中。
我希望用户能够上传视频文件,然后将其发布在我的 YouTube 频道上。
我正在使用带有 Youtube_it gem 的 Rails 4。我一直在尝试遵循本教程:http://www.sitepoint.com/youtube-rails/
我的文件结构有项目模型和视频模型。这些关联是:
项目.rb
has_one :video
accepts_nested_attributes_for :video
视频.rb
belongs_to :project
我的视频表有一个名为 :include_video 的布尔属性。
<%= f.collection_radio_buttons :include_video, [[true, ' Yes'], [false, ' No']], :first, :last, {:item_wrapper_class => 'fixradio'}, {:class => "radio-inline create-project response-project"} %>
如果是真的,那么我想揭示用户添加视频链接的字段(属性称为:链接)。
<%= f.file_field :link %>
我提出这些问题的视频形式是片面的。部分在有其他问题的项目表格内。我还有一个局部视图,其中包含你管夹的容器(如教程中所示)。
视图部分包含在我的项目显示页面中:
<% if @project.video.include_video? %>
<%= render 'videos/particulars' %>
<% end %>
我的问题是,当我尝试这种方法时,我收到一条错误消息:
undefined method `include_video?' for nil:NilClass
我不明白 undefined method 在我的视频表中属性名称的上下文中是什么意思,我也不明白 nil:NilClass 是什么意思。
我也尝试通过以下方式包含视频视图部分:
<% if @project.video.try(:include_video) %>
<%= render 'videos/particulars' %>
<% end %>
这不会给出错误消息,但也不会显示剪辑(当我在代码检查器中检查元素时,它根本没有显示。我可以看到上传也没有保存到数据库,因为我的控制台搜索显示没有视频。
我的项目控制器具有来自我的视频表的白标属性(我的视频控制器也是如此。
def project_params
params.require(:project).permit(
video_attributes: [:include_video, :link],
def video_params
params[:video].permit(:include_video, :link, :project_id)
end
我该如何解决这个问题?
我的 video.rb 有:
class Video < ActiveRecord::Base
# --------------- associations
belongs_to :project
belongs_to :program
belongs_to :proposal
belongs_to :profile
belongs_to :user
# --------------- scopes
# --------------- validations
YT_LINK_FORMAT = /\A.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*\z/i
validates :link, presence: true, format: YT_LINK_FORMAT
# --------------- class methods
# --------------- callbacks
before_create -> do
uid = link.match(YT_LINK_FORMAT)
self.uid = uid[2] if uid && uid[2]
if self.uid.to_s.length != 11
self.errors.add(:link, 'is invalid.')
false
elsif Video.where(uid: self.uid).any?
self.errors.add(:link, 'is not unique.')
false
else
get_additional_info
end
end
# --------------- instance methods
# --------------- private methods
def get_additional_info
begin
client = YouTubeIt::OAuth2Client.new(dev_key: ENV['YT_developer_key'])
video = client.video_by(uid)
self.title = video.title
self.duration = parse_duration(video.duration)
self.author = video.author.name
self.likes = video.rating.likes
rescue
self.title = '' ; self.duration = '00:00:00' ; self.author = '' ; self.likes = 0 ; self.dislikes = 0
end
end
def parse_duration(d)
hr = (d / 3600).floor
min = ((d - (hr * 3600)) / 60).floor
sec = (d - (hr * 3600) - (min * 60)).floor
hr = '0' + hr.to_s if hr.to_i < 10
min = '0' + min.to_s if min.to_i < 10
sec = '0' + sec.to_s if sec.to_i < 10
hr.to_s + ':' + min.to_s + ':' + sec.to_s
end
end
我的 project_controller 有:
def new
@project = Project.new
@project.video = Video.new
end
我的 video_controller 有:
def show
respond_with(@video)
end
def create
@video = Video.new(video_params)
@video.project_id = video_params[:project_id]
end
【问题讨论】:
-
Youtube 最近更改了他们的公共 api,我建议您对此进行一些调查,因为这实际上可能是问题所在。我最近也遇到了
youtube_itgem 的问题。
标签: ruby-on-rails youtube