0

Railsの初心者として、私は自分の問題を解決する方法を見つけることができません^^
ビデオのURLを含むテキストフィールドを持つフォームからVideoPostを作成したい(YouTubeなど)
宝石のおかげでビデオに関する情報を取得していますhttps://github.com/thibaudgg/video_info
そして、私のモデル (VideoInformation) を使用してそれらの情報を保存したいと思います。しかし、作成プロセスがどのように機能するかわかりません。
助けてくれてありがとう!

次のように VideoPostsController で VideoPost を作成しようとしています:

def create
  video_info = VideoInfo.new(params[:video_url])
  video_information = VideoInformation.create(video_info)      #undefined method `stringify_keys' for #<Youtube:0x00000006a24120>
  if video_information.save
    @video_post = current_user.video_posts.build(video_information) 
  end
end

私の VideoPost モデル:

# Table name: video_posts
#
#  id                   :integer          not null, primary key
#  user_id              :integer
#  video_information_id :integer
#  created_at           :datetime         not null
#  updated_at           :datetime         not null

私の VideoInformation モデル (VideoInfo gem と同じ属性名を取得):

# Table name: video_informations
#
#  id              :integer          not null, primary key
#  title           :string(255)
#  description     :text
#  keywords        :text
#  duration        :integer
#  video_url       :string(255)
#  thumbnail_small :string(255)
#  thumbnail_large :string(255)
#  created_at      :datetime         not null
#  updated_at      :datetime         not null
4

2 に答える 2

3

作成プロセスがどのように機能するかがわからない

createメソッドは、任意のオブジェクトではなく、パラメーター付きのハッシュを想定しています。VideoInfo のメソッドを使用して、ActiveRecord で使用できるハッシュに変換する必要があります。

于 2012-10-28T22:57:02.793 に答える
2

VideoInformation モデルにメソッドを追加して、video_info を渡すことで作成できるようにします。

# app/models/video_information.rb
def self.create_from_video_info(video_info, url)
  video_information = self.new
  video_information.title = video_info.title
  video_information.description = video_info.description
  video_information.keywords = video_info.keywords
  video_information.duration = video_info.duration
  # video_url appears to not be available on video_info,
  # maybe you meant embed_url?
  video_information.video_url = url
  video_information.thumbnail_small = video_info.thumbnail_small
  video_information.thumbnail_large = video_info.thumbnail_large
  video_information.save
  video_information
end

# app/controllers/video_posts_controller.rb
def create
  video_info = VideoInfo.new(params[:video_url])
  video_information = VideoInformation.create_from_video_info(video_info, params[:video_url])

  if video_information.valid?
    current_user.video_posts << video_information
  end
end

また、これを別の方法で行うことを検討することもできます。VideoInformationVideoInfoおよびVideoPostクラス を持つのは冗長に思えます。

おそらく、モデルはビデオの URL を単純に保存し、インスタンスをレンダリング/使用するときに必要に応じてその場でVideoPostプルすることができます。VideoInfoVideoPost

于 2012-10-28T23:15:25.943 に答える