Ruby on rails 在“从类创建操作”中创建模型

Ruby on rails 在“从类创建操作”中创建模型,ruby-on-rails,ruby,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby,Ruby On Rails 3,Activerecord,作为rails的新手,我找不到如何解决我的问题^^ 我想从包含视频url(如youtube)的文本字段的表单创建VideoPost 多亏了gem,我才获得了视频信息 我想用我的模型(视频信息)保存这些信息。 但我不知道创建过程应该如何工作。 谢谢你的帮助 我正在尝试在VideoPostsController中创建VideoPost,如下所示: def create video_info = VideoInfo.new(params[:video_url]) video_informati

作为rails的新手,我找不到如何解决我的问题^^
我想从包含视频url(如youtube)的文本字段的表单创建VideoPost
多亏了gem,我才获得了视频信息
我想用我的模型(视频信息)保存这些信息。 但我不知道创建过程应该如何工作。
谢谢你的帮助

我正在尝试在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
我的VideoInformation模型(其属性名称与VideoInfo gem相同):

不知道创建过程应该如何工作

create
方法需要一个带有参数的散列,而不是一些任意对象。您应该使用VideoInfo的方法,并将其转换为ActiveRecord可以使用的哈希

不知道创建过程应该如何工作


create
方法需要一个带有参数的散列,而不是一些任意对象。您应该使用VideoInfo的方法,并将其转换为可由ActiveRecord使用的哈希值。

我将向VideoInformation模型添加一个方法,以便您可以通过传入VideoInfo创建一个方法:

# 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
#app/models/video_information.rb
定义self.create_from_video_info(视频信息,url)
视频信息=self.new
video\u information.title=video\u info.title
video\u information.description=video\u info.description
video\u information.keywords=video\u info.keywords
video\u information.duration=video\u info.duration
#视频信息中似乎没有视频url,
#也许你的意思是嵌入url?
video\u information.video\u url=url
video\u information.thumboil\u small=video\u info.thumboil\u small
video\u information.thumboil\u large=video\u info.thumboil\u large
视频信息保存
视频信息
结束
#app/controllers/video_posts_controller.rb
def创建
video\u info=VideoInfo.new(参数[:video\u url])
视频信息=视频信息。从视频信息创建视频信息(视频信息,参数[:视频url])
视频信息是否有效?

current_user.video_posts我将向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
#app/models/video_information.rb
定义self.create_from_video_info(视频信息,url)
视频信息=self.new
video\u information.title=video\u info.title
video\u information.description=video\u info.description
video\u information.keywords=video\u info.keywords
video\u information.duration=video\u info.duration
#视频信息中似乎没有视频url,
#也许你的意思是嵌入url?
video\u information.video\u url=url
video\u information.thumboil\u small=video\u info.thumboil\u small
video\u information.thumboil\u large=video\u info.thumboil\u large
视频信息保存
视频信息
结束
#app/controllers/video_posts_controller.rb
def创建
video\u info=VideoInfo.new(参数[:video\u url])
视频信息=视频信息。从视频信息创建视频信息(视频信息,参数[:视频url])
视频信息是否有效?

当前用户。视频帖子谢谢你的回复。在VideoInfo中没有这样的方法,我想,我尝试了video_information=VideoInformation.new(video_info.attributes),但它返回了错误“未定义的方法` attributes'for#”。如何将其转换为哈希?(事实上,我认为rails是自己隐式完成的)有一些方法可以获取单个信息。您可以使用它们来构建哈希。例如,请参阅@JasonNoble的答案。我在想rails有一种优雅而简单的方法来做到这一点:/谢谢你的回复。在VideoInfo中没有这样的方法,我想,我尝试了video_information=VideoInformation.new(video_info.attributes),但它返回了错误“未定义的方法` attributes'for#”。如何将其转换为哈希?(事实上,我认为rails是自己隐式完成的)有一些方法可以获取单个信息。您可以使用它们来构建哈希。例如,请参阅@JasonNoble的答案。我在想rails有一种优雅而简单的方法来做到这一点:/谢谢你的回复。这可能有用,我要试试。但我认为rails有一种优雅而简单的方法来实现这一点:/我有两种不同的模型,因为我计划在将来在VideoInformation中添加关联^^(这样我就避免每次调用youtube api)VideoU url不必存储在VideoInfo中,而是用于创建对象的新页面上表单的值。用法:VideoInfo.new(“)谢谢你的回复。这可能有用,我会试试。但我认为rails有一种优雅而简单的方法来做到这一点:/我有两种不同的模型,因为我计划在将来在VideoInformation中添加关联^^(这样我就避免了每次调用youtube api)video\u url不必存储在VideoInfo中,它是新页面上用于创建对象的表单的值。用法:VideoInfo.new(“)
# 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