ruby-on-rails – Rails 4:整合You Tube(youtube_it gem)

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails 4:整合You Tube(youtube_it gem)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图把你的管插入我的rails 4应用程序.

我希望用户能够上传一个视频文件,然后将其发布在我的管道上.

我正在使用Rails 4和Youtube_it宝石.我一直在试图遵循这个教程:http://www.sitepoint.com/youtube-rails/

我的文件结构有项目模型和视频模型.协会是:

Project.rb

  1. has_one :video
  2. accepts_nested_attributes_for :video

Video.rb

  1. belongs_to :project

我的视频表有一个布尔属性,名为:include_video.

  1. <%= f.collection_radio_buttons :include_video,[[true,' Yes'],[false,' No']],:first,:last,{:item_wrapper_class => 'fixradio'},{:class => "radio-inline create-project response-project"} %>

如果是真的,那么我想透露用户添加视频链接的字段(属性称为:link).

  1. <%= f.file_field :link %>

我提出这些问题的视频表格是部分的.部分是在项目形式内还有其他问题.我也有一个部分视图,它具有您的管夹的容器(如教程所示).

视图部分包含在我的项目显示页面中:

  1. <% if @project.video.include_video? %>
  2.  
  3. <%= render 'videos/particulars' %>
  4. <% end %>

我的项目控制人有:

  1. def new
  2. @project = Project.new
  3. @project.video = Video.new
  4. end
  5. def show
  6. #authorise @project
  7. @project = Project.find(params[:id])
  8. @creator = User.find(@project.creator_id)
  9. @creator_profile = @creator.profile
  10. @project.video ||= Video.new
  11.  
  12. end

我的项目控制器也有我的视频表中的白色标签属性(和我的视频控制器一样).

  1. def project_params
  2. params.require(:project).permit(
  3. video_attributes: [:include_video,:link],

我的视频控制器有:

  1. def show
  2. respond_with(@video)
  3. end
  4.  
  5.  
  6. def create
  7.  
  8. @video = Video.new(video_params)
  9. @video.project_id = video_params[:project_id]
  10.  
  11. end
  12.  
  13. def video_params
  14. params[:video].permit(:include_video,:link,:project_id)
  15. end

我的video.rb有:

  1. class Video < ActiveRecord::Base
  2.  
  3. # --------------- associations
  4. belongs_to :project
  5. belongs_to :program
  6. belongs_to :proposal
  7. belongs_to :profile
  8. belongs_to :user
  9.  
  10.  
  11. # --------------- scopes
  12. # --------------- validations
  13.  
  14. YT_LINK_FORMAT = /\A.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*\z/i
  15.  
  16. validates :link,presence: true,format: YT_LINK_FORMAT
  17.  
  18.  
  19. # --------------- class methods
  20.  
  21. # --------------- callbacks
  22.  
  23. before_create -> do
  24. uid = link.match(YT_LINK_FORMAT)
  25. self.uid = uid[2] if uid && uid[2]
  26.  
  27. if self.uid.to_s.length != 11
  28. self.errors.add(:link,'is invalid.')
  29. false
  30. elsif Video.where(uid: self.uid).any?
  31. self.errors.add(:link,'is not unique.')
  32. false
  33. else
  34. get_additional_info
  35. end
  36. end
  37.  
  38. # --------------- instance methods
  39.  
  40. # --------------- private methods
  41.  
  42. def get_additional_info
  43. begin
  44. client = YouTubeIt::OAuth2Client.new(dev_key: ENV['YT_developer_key'])
  45. video = client.video_by(uid)
  46. self.title = video.title
  47. self.duration = parse_duration(video.duration)
  48. self.author = video.author.name
  49. self.likes = video.rating.likes
  50. rescue
  51. self.title = '' ; self.duration = '00:00:00' ; self.author = '' ; self.likes = 0 ; self.dislikes = 0
  52. end
  53. end
  54.  
  55. def parse_duration(d)
  56. hr = (d / 3600).floor
  57. min = ((d - (hr * 3600)) / 60).floor
  58. sec = (d - (hr * 3600) - (min * 60)).floor
  59.  
  60. hr = '0' + hr.to_s if hr.to_i < 10
  61. min = '0' + min.to_s if min.to_i < 10
  62. sec = '0' + sec.to_s if sec.to_i < 10
  63.  
  64. hr.to_s + ':' + min.to_s + ':' + sec.to_s
  65. end
  66. end

我有一个相关的问题,并得到这个职位的帮助:Rails 4 with You Tube

但是,它创造了一个新的问题,我不知道它是否正在推进我的解决方案的进展.

新的一点(从用户对另一个发布的问题提出的建议)是将视频添加到我的项目控制器的显示行.当我尝试这个,我得到这个错误

  1. Failed to save the new associated video.

当我尝试制作一个全新的项目,其中包含视频表单时,我会收到一个错误保存表单.它说视频链接无效(我从我的硬盘驱动器中选择一个.mov文件).

然后我将我的video.rb中的验证更改为:

  1. validates :link,format: YT_LINK_FORMAT,:allow_blank => true,:allow_nil => true

(即删除:存在:true,并添加空白和零,但我仍然收到该错误)

如何解决这个问题?

解决方法

我不会使用youtube_it ruby​​ gem,因为它使用了v2版的YouTube API(从2015年4月20日起不支持).我建议使用yt ruby​​ gem(使用最新版本的YouTube API).

A tutorial on the yt gem and why you shouldn’t use youtube_it

您的问题很可能与尝试使用YouTube API不支持的youtube_it功能有关.重构代码似乎有很多工作要做,但是我确定它可以节省你很多头痛的东西(尤其是因为youtube_it看起来没有很好的维护).

猜你在找的Ruby相关文章