Rails 4 个与 Youtube

Rails 4 with You Tube

我正在尝试将你的电子管合并到我的 rails 4 应用程序中。

我希望用户能够上传一个视频文件,该文件将发布在我的 youtube 频道上。

我正在使用 Rails 4 和 Youtube_it gem。我一直在尝试遵循本教程:http://www.sitepoint.com/youtube-rails/

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

Project.rb

has_one :video
  accepts_nested_attributes_for :video

Video.rb

  belongs_to :project

我的视频 table 有一个名为 :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"} %>

如果是真的,那么我想揭示用户在视频中添加link的字段(属性称为:link)。

        <%= f.file_field :link %>

我提出这些问题的视频形式是部分的。部分在有其他问题的项目表格内。我还有一个局部视图,其中包含用于管夹的容器(如教程中所示)。

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

<% if @project.video.include_video? %>

          <%= render 'videos/particulars' %>
    <% end %>

我的问题是,当我尝试这种方法时,我收到一条错误消息:

undefined method `include_video?' for nil:NilClass

我不明白未定义的方法在我的视频中的属性名称的上下文中是什么意思 table 我也不明白 nil:NilClass 是什么意思。

我也试过包括视频观看部分:

<% if @project.video.try(:include_video) %>

          <%= render 'videos/particulars' %>
    <% end %>

这不会给出错误消息,但也不会显示剪辑(当我在代码检查器中检查元素时,它根本没有显示。我可以看到上传也没有' t 保存到数据库,因为我的控制台搜索说没有视频。

我的项目控制器具有来自我的视频的白色标签属性 table(我的视频控制器也是如此。

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

   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

我的 project_controller 有:

def new
    @project = Project.new
    @project.video = Video.new
end

我的 video_controller 有:

  def show
    respond_with(@video)
  end


 def create

    @video = Video.new(video_params)
    @video.project_id = video_params[:project_id]

  end

出现此错误:“未定义的方法 include_video?' for nil:NilClass" - 它告诉您您正在 nil 上调用 include_video?。这反过来又告诉您 [=13= 的值] 是零,当这个错误发生时。所以,这就是正在发生的事情。

下一个要解决的问题是 "Why does @project.video evaluate to nil?"。您的显示页面模板在项目控制器的 show 操作的上下文中运行。此操作是否为@project.video设置了值?

你可以做的,在页面中是这样的:

<%= @project.video ||= Video.new %>

如果它已经存在,这将保持不变,但如果它不存在,它将定义它。 ||= 是 shorthand 对于 "equals itself or",所以上面的等价于:

<%= @project.video = @project.video || Video.new %>