我一直在努力弄清楚如何使用YT Gem上传缩略图。有谁知道如何帮忙?这是我正在关注的原始教程,但它没有谈论自定义缩略图。 [https://www.sitepoint.com/youtube-api-version-3-rails/][1] 我可以上传视频而不是自定义缩略图。
非常感谢您的帮助..
视频上传模型
class VideoUpload < ActiveType::Object
attribute :file, :varchar
attribute :title, :varchar
attribute :description, :text
attribute :upload_thumbnail, :varchar
validates :file, presence: true
validates :title, presence: true
validates :upload_thumbnail, presence: true
def upload!(user)
account = Yt::Account.new access_token: user.token
account.upload_video self.file, title: self.title, description: self.description, upload_thumbnail: self.upload_thumbnail
end
end
VideoUploadController
def create
@video_upload = VideoUpload.new(
title: params[:video_upload][:title],
description: params[:video_upload][:description],
upload_thumbnail: params[:video_upload][:upload_thumbnail],
file: params[:video_upload][:file].try(:tempfile).try(:to_path)
)
if @video_upload.save
uploaded_video = @video_upload.upload!(current_user)
if uploaded_video.failed?
flash[:error] = 'There was an error while uploading your video...'
else
Video.create({
link: "https://www.youtube.com/watch?v=#{uploaded_video.id}",
uid: uploaded_video.id,
title: uploaded_video.title,
description: uploaded_video.description
})
flash[:success] = 'Your video has been uploaded!'
end
redirect_to root_url
else
render :new
end
end
查看
<%= form_for @video_upload do |f| %>
<%= render 'shared/errors', object: @video_upload %>
<div class="form-group">
<%= f.label :file %>
<%= f.file_field :file, class: 'form-control', required: true %>
</div>
<div class="form-group">
<%= f.label :upload_thumbnail %>
<%= f.file_field :upload_thumbnail, class: 'form-control', required: true %>
</div>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control', required: true %>
</div>
<div class="form-group">
<%= f.label :description %>
<%= f.text_area :description, class: 'form-control', cols: 3 %>
</div>
<%= f.submit 'Upload', class: 'btn btn-primary' %>
<% end %>
服务器响应
VideoUploadsController处理#create as HTML 参数:{“utf8”=&gt;“✓”,“authenticity_token”=&gt;“wFf3EbDhQjJwttjnE + nbQxinH508coHBctC + hi4M4sNAyhe6fisDhGWscVDxppS9NJ3 / fcjCGQKiNFy1thAXOA ==”, “video_upload”=&GT; { “文件”=&GT;#, @ original_filename =“IMG_0673.MOV”,@ content_type =“video / quicktime”, @ headers =“Content-Disposition:form-data; name = \”video_upload [file] \“; filename = \“IMG_0673.MOV \”\ r \ nConContent-Type:video / quicktime \ r \ n“&gt;, “upload_thumbnail”=&GT;#, @ original_filename =“protest_3.png”,@ content_type =“image / png”, @ headers =“Content-Disposition:form-data; 名= \ “video_upload [upload_thumbnail] \”; filename = \“protest_3.png \”\ r \ nConContent-Type:image / png \ r \ n“&gt;, “title”=&gt;“o iuiou goiug oiug piu gpiugpiu”,“description”=&gt;“yg ouyg ouyg piu gpugpiu piuhpiuhpiuh [io hio h; ['o ih [oi [oih ['oih“}, “提交”=&gt; “中上传”}
更新控制器:
def create
@video_upload = VideoUpload.new(video_upload_params)
[:file].try(:tempfile).try(:to_path))
if @video_upload.save
video = video_upload_params[:file].try(:tempfile).try(:to_path)
account = Yt::Account.new access_token: current_user.token
uploaded_video = account.upload_video(video, video_upload_params)
if uploaded_video.failed?
render :new
flash[:error] = 'There was an error while uploading your video...'
else
Video.create({link: "https://www.youtube.com/watch?v=#{uploaded_video.id}",
uid: uploaded_video.id, title: uploaded_video.title, description: uploaded_video.description})
flash[:success] = 'Your video has been uploaded!'
end
redirect_to videos_path
else
render :new
end
end
private
def video_upload_params
params.require(:video_upload).permit(:file, :title, :description, :upload_thumbnail)
end
VideoUploadsController处理#create as HTML 参数:{“utf8”=&gt;“✓”,“authenticity_token”=&gt;“V1qozopsKc8Cb9 / 99ex4GME2O6Z5MFDaPQhiILA5fLfXx0hlRKZoeRd1dkoXozfm7QzbRo2AyBnt7IATKCWJTA ==”,“video_upload”=&gt; {“file”=&gt;#,@ original_filename =“IMG_0673.MOV” ,@ content_type =“video / quicktime”,@ headers =“Content-Disposition:form-data; name = \”video_upload [file] \“; filename = \”IMG_0673.MOV \“\ r \ nConContent-Type:video / quicktime \ r \ n“&gt;,”upload_thumbnail“=&gt;#,@ original_filename =”protest_3.png“,@ content_type =”image / png“,@ headers =”Content-Disposition:form-data; name = \“video_upload [upload_thumbnail] \”; filename = \“protest_3.png \”\ r \ nConContent-Type:image / png \ r \ n“&gt;,”title“=&gt;”; lekhjg wktrj hoibwj 5tor [fij [boij r [oijj [goj [oi j“,”description“=&gt;”woer jgowiejr [ihjg w [oei fgoi joifj g [okj fskdjfa; lkj f; klajs d; lfkdaj sd; lkfj a; lskdjf lakjs df; lkaj] sdk e; lkrj; gkj4 roij toi2j 45oirj toij 45oijrtij 54ij = tioj = j5 = oi ejwrgoij wroiejh gpoiej rtpoihj ep5iojt rpoibj5 t“},”commit“=&gt;”Upload“}
答案 0 :(得分:0)
获取请求
GET "videouploads/new", "videouploads#new"
服务器呈现videouploads/new.html.erb
和表单字段@videoupload.upload_thumbnail
<div class="form-group">
<%= f.label :upload_thumbnail %>
<%= f.file_field :upload_thumbnail, class: 'form-control', required: true %>
</div>
用户填写表单并提交
具有强参数的控制器允许读取这些参数。这是参数,您需要关注"upload_thumbnail"
。
您可以在此处传递视频游戏的值。它包含"upload_thumbnail"
,@original_filename
等其他字段。
{"utf8"=>"✓",
"authenticity_token"=>"somecode",
"video_upload"=>{
..a sequence of fields then..,
"upload_thumbnail"=>#,
@original_filename="protest_3.png",
@content_type="image/png",
@headers="Content-Disposition: form-data;
name=\"video_upload[upload_thumbnail]\";
filename=\"protest_3.png\"\r\nContent-Type: image/png\r\n">,
"title"=>"o iuiou goiug oiug piu gpiugpiu ",
"description"=>"yg ouyg ouyg piu gpugpiu piuhpiuhpiuh [ io hio h; ['o ih[oi [oih [' oih "},
"commit"=>"Upload"
}
使用步骤4中路由器传递的参数创建视频上传实例,params
将是以上内容:
@video_upload = VideoUpload.new(params)
问题可能您没有使用整个参数 params[:video_upload]
,只是
upload_thumbnail: params[:video_upload][:upload_thumbnail]
从上面可以看出,它将等于:
"upload_thumbnail"=>#,
缩略图还有许多其他字段,例如
@original_filename="protest_3.png",
@content_type="image/png",
@headers="Content-Disposition: form-data;
name=\"video_upload[upload_thumbnail]\";
filename=\"protest_3.png\"\r\nContent-Type: image/png\r\n">,
"title"=>"o iuiou goiug oiug piu gpiugpiu ",
"description"=>"yg ouyg ouyg piu gpugpiu piuhpiuhpiuh [ io hio h; ['o ih[oi [oih [' oih "},
"commit"=>"Upload"
因此,使用所有参数
创建对象可能是个好主意@video_upload = VideoUpload.new(your_strong_params)
你需要使用rails strong params
http://weblog.rubyonrails.org/2012/3/21/strong-parameters/
然后保存实例video_upload
@video_upload.save
如果这不能解决问题,您需要使用调试器停止或使用put
ruby语法在服务器上打印以下字段:
puts @video_upload.errors.full_messages
或者通过调试检查出来并告诉我们您是否有任何错误消息,因为可能您没有保存它...或者可能