【发布时间】:2015-09-17 05:45:18
【问题描述】:
我正在尝试将你的 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 %>
我的项目控制器有:
def new
@project = Project.new
@project.video = Video.new
end
def show
#authorise @project
@project = Project.find(params[:id])
@creator = User.find(@project.creator_id)
@creator_profile = @creator.profile
@project.video ||= Video.new
end
我的项目控制器还具有来自我的视频表的白标属性(我的视频控制器也是如此。
def project_params
params.require(:project).permit(
video_attributes: [:include_video, :link],
我的视频控制器有:
def show
respond_with(@video)
end
def create
@video = Video.new(video_params)
@video.project_id = video_params[:project_id]
end
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
我遇到了一个相关的问题,并从这个帖子中获得了帮助:Rails 4 with You Tube
但是,它产生了一个新问题,我不知道它是否正在推动我朝着解决方案前进。
新的一点(来自用户在另一个发布的问题上提出的建议)是在我的项目控制器中添加一条显示线以用于视频。当我尝试这个时,我得到了这个错误:
Failed to save the new associated video.
当我尝试制作一个包含部分视频表单的全新项目时,我在保存表单时遇到错误。它说视频链接无效(我正在从我的硬盘驱动器中选择一个 .mov 文件)。
然后我将 video.rb 中的验证更改为:
validates :link, format: YT_LINK_FORMAT, :allow_blank => true, :allow_nil => true
(即删除:存在:真,并添加空白和零,但我仍然得到那个错误)
我该如何解决这个问题?
【问题讨论】:
标签: ruby-on-rails video youtube