提问者:小点点

Rails 使用 YT Gem 从应用程序上传 YouTube 缩略图


我在尝试弄清楚如何使用 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

视频上传控制器

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”=

更新的控制器:

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”=


共1个答案

匿名用户

>

  • 获取请求

    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 中从路由器传递的参数创建视频上传实例,参数将是上面的参数

    @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)
    

    你需要使用轨道强参数

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

    然后将实例保存video_upload

    @video_upload.save
    

    如果这不能解决问题,则需要停止使用调试器或使用 put ruby 语法将以下字段打印到服务器:

    puts @video_upload.errors.full_messages
    

    或者通过调试检查这些内容,并告诉我们您是否有任何错误消息,因为您可能没有保存它。或者也许