Rails 4 with You Tube [英] Rails 4 with You Tube

查看:46
本文介绍了Rails 4 with You Tube的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将您的 Tube 合并到我的 rails 4 应用程序中.

I am trying to incorporate you tube in my rails 4 app.

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

I want users to be able to upload a video file which will then be posted on my you tube channel.

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

I am using Rails 4 with Youtube_it gem. I have been trying to follow this tutorial: http://www.sitepoint.com/youtube-rails/

我的文件结构有项目模型和视频模型.这些关联是:

My file structure has project model and a video model. The associations are:

项目.rb

has_one :video
  accepts_nested_attributes_for :video

视频.rb

  belongs_to :project

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

My video table has a boolean attribute called :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).

If it's true, then I want to reveal the field where users add a link to the video (attribute is called :link).

        <%= f.file_field :link %>

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

The video form in which I ask these questions is a partial. The partial is inside the project form which has other questions. I also have a partial view which has the container for the you tube clip (as shown in the tutorial).

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

The view partial is included in my project show page as:

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

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

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

My problem is that when I try this approach, I get an error that says:

undefined method `include_video?' for nil:NilClass

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

I don't understand what undefined method means in the context of the name of an attribute in my video table and I also don't understand what nil:NilClass means.

我还尝试通过以下方式包含视频视图部分:

I have also tried including the video view partial by:

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

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

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

This does not give an error message but it also doesn't show the clip (and when I inspect the element in the code inspector, it isn't shown at all. I can see that the upload also hasn't saved to the db, because my console search says there are no videos.

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

My project controller has white labelled attributes from my video table (as does my video controller.

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

   def video_params
      params[:video].permit(:include_video, :link, :project_id)
    end

我该如何解决这个问题?

How do I approach figuring this out?

我的 video.rb 有:

My video.rb has:

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 有:

My project_controller has:

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

我的 video_controller 有:

My video_controller has:

  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?.当这个错误发生时,它反过来告诉你 @project.video 的值是 nil.所以,这就是正在发生的事情.

with this error: "undefined method include_video?' for nil:NilClass" - it's telling you that you are calling include_video? on nil. Which in turn tells you that the value of @project.video is nil, when this error occurs. So, that's what's happening.

下一个要解决的问题是为什么@project.video 的计算结果为零?".您的显示页面模板在项目控制器的 show 操作的上下文中运行.此操作是否为 @project.video 设置了值?

The next question to address is "Why does @project.video evaluate to nil?". Your show page template runs inside the context of the Project controller's show action. Has this action set a value for @project.video?

你可以做什么,在页面上是这样的:

What you could do, in the page is this:

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

如果它已经存在,这将保持不变,但如果它不存在,它将定义它.||= 是等于自身或"的简写,所以上面的等价于:

This will leave it unchanged if it already exists, but if it doesn't exist it will define it. ||= is shorthand for "equals itself or", so the above is the equivalent of this:

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

这篇关于Rails 4 with You Tube的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆