2017-05-28 277 views
0

我一直在嘗試弄清如何使用YT Gem上傳縮略圖時遇到了很多麻煩。有誰知道如何提供幫助?這是我正在關注的原始教程,但它沒有提到自定義縮略圖。 [https://www.sitepoint.com/youtube-api-version-3-rails/][1] 我可以上傳視頻而不是自定義縮略圖。Rails使用YT從應用上傳YouTube縮略圖Gem

非常感謝您的幫助..

視頻上傳模式

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#創建以HTML 參數:{ 「UTF8」=> 「✓」, 「authenticity_token」=> 「wFf3EbDhQjJwttjnE + nbQxinH508coHBctC + hi4M4sNAyhe6fisDhGWscVDxppS9NJ3/fcjCGQKiNFy1thAXOA ==」, 「video_upload」=> { 「file」=>#, @ original_filename =「IMG_0673.MOV」,@ content_type =「video/quicktime」, @ headers =「Content-Disposition:form-data;命名= \ 「video_upload [文件] \」; filename = \「IMG_0673.MOV \」\ r \ nContent-Type:video/quicktime \ r \ n「>, 」upload_thumbnail「=>#, @ original_filename =」protest_3.png「,@ content_type =」image/png「, @ headers =」Content-Disposition:form-data; name = \「video_upload [upload_thumbnail] \」;內容類型:image/png \ r \ n「>, ouyg piu gpugpiu piuhpiuhpiuh [io hio h; [ 'O IH [OI [OIH [' OIH 「}, 」提交「=>」 上傳「}

修訂CONTROLLER:

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#創建以HTML 參數:{「utf8」=>「✓」,「authenticity_token」=>「V1qozopsKc8Cb9/99ex4GME2O6Z5MFDaPQhiILA5fLfXx0hlRKZoeRd1dkoXozfm7QzbRo2AyBnt7IATKCWJTA ==」,「video_upload」=> {「file」=>#,@ original_filename =「IMG_0673.MOV」,@ content_type = 「video/quicktime」,@ headers =「Content-Disposition:form-data;命名= \ 「video_upload [文件] \」; filename = \「IMG_0673.MOV \」\ r \ nContent-Type:video/quicktime \ r \ n「>,」upload_thumbnail「=>#,@ original_filename =」protest_3.png「,@ content_type =」image/png「 ,@ headers =「Content-Disposition:form-data;名= \ 「video_upload [upload_thumbnail] \」;文件名= \「protest_3。png \「\ r \ nContent-Type:image/png \ r \ n」>,「title」=>「; lekhjg wktrj hoibwj 5tor [fij [boij r [oifj [goj [oi j」,「description」=> 「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噸 「}, 」提交「=>」 上傳「}

+0

你的服務器日誌中是否有錯誤信息? –

+0

嗨。感謝您的迴應。我似乎沒有得到一個錯誤。 雖然我附加了我的服務器迴應我的問題。 –

+0

你如何傳遞你的參數的自定義縮略圖? –

回答

0
  1. GET請求

    GET "videouploads/new", "videouploads#new" 
    
  2. 服務器呈現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> 
    
  3. 用戶填寫表單並提交

  4. 您控制器具有較強的PARAMS允許讀取這些參數。這是參數,您需要關注"upload_thumbnail"。 這是你值傳遞給你的視頻thubnail。它包括像"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" 
         } 
    
  5. 與路由器在步驟4中通過PARAMS創建視頻上傳實例,該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) 

你需要使用軌強PARAMS

http://weblog.rubyonrails.org/2012/3/21/strong-parameters/

  • 然後保存實例video_upload

    @video_upload.save 
    
  • 如果這不解決問題,您需要停止使用調試器或使用打印到服務器的Ruby語法以下字段:

    puts @video_upload.errors.full_messages 
    

    或檢查出那些與調試,並告訴我們,如果您有任何錯誤消息,因爲很可能你不保存它..或者可能

    +0

    謝謝你的迴應。不幸的是,它仍然不起作用。我追加了我更新的控制器和服務器響應。我正在嘗試學習如何使用錯誤消息。我也將嘗試看看我是否可以使用更新操作來工作,因爲這是我在示例中看到的。 –

    +0

    @MatthewWeinkle好的,讓我知道你是否想分享錯誤的更多細節,如果你需要幫助!謝謝 。祝你好運法布里奇奧 –

    相關問題