Ruby on rails 如何避免Errno::EISDIR:在运行种子迁移时是目录读取吗?

Ruby on rails 如何避免Errno::EISDIR:在运行种子迁移时是目录读取吗?,ruby-on-rails,ruby,yaml,errno,Ruby On Rails,Ruby,Yaml,Errno,我很难从YAML文件上传种子-在尝试上传没有图片的帖子之前,一切正常(图片:nil) 和YAML文件: - title: 'Title1' content: 'some content for post' created_at: updated_at: deleted_at: post_img: '/image1jpg' - title: 'Title 2' content: 'some content for post' created_at:

我很难从YAML文件上传种子-在尝试上传没有图片的帖子之前,一切正常(图片:nil)

和YAML文件:

-
  title: 'Title1'
  content: 'some content for post'
  created_at: 
  updated_at:
  deleted_at:
  post_img: '/image1jpg'


-
  title: 'Title 2'
  content: 'some content for post'
  created_at: 
  updated_at:
  deleted_at:
  post_img:
如果我同时填写两个post_img字段,它可以正常工作,但当其中一个字段为空时,会出现以下错误:

Errno::EISDIR:是一个目录


这意味着它读取整个图像文件夹。如何找到避免此错误的方法?

问题是当
post\u img
为空/nil时,
File.open(“{images\u path}}{post['post\u img']}”)
是一个目录(
images\u path
),而不是错误消息所说的文件。您可以执行以下操作:

  file = File.open("#{images_path}#{post['post_img']}") if post['post_img'].present?
  Post.create(
      title: post['title'],
      content: post['content'],
      created_at: post['created_at'],
      updated_at: post['updated_at'],
      deleted_at: post['deleted_at'],
      post_img: file
  )
如果
post\u img
为空/nil,这将创建一个带有nil图像的post

  file = File.open("#{images_path}#{post['post_img']}") if post['post_img'].present?
  Post.create(
      title: post['title'],
      content: post['content'],
      created_at: post['created_at'],
      updated_at: post['updated_at'],
      deleted_at: post['deleted_at'],
      post_img: file
  )