0

我正在尝试使用carrierwave-base64 和accepts_nested_attributes_for 上传文件,但出现“文件不能为空”错误。这是我正在尝试的示例:

post.rb
has_many :post_files, dependent: :destroy
accepts_nested_attributes_for :post_files, reject_if: :all_blank, allow_destroy: :true

post_file.rb
validates :file, presence: true
mount_base64_uploader :file, AvatarUploader, file_name: -> (e) { e.filename }

posts_controller.rb
@post = Post.new(post_params)
...
@post.save

但是如果我像下面的代码那样做,它会起作用:

@post = Post.new(post_params.except(:post_files_attributes))

post_params[:post_files_attributes].each do |attributes|
  if !attributes["_destroy"]
    @post.post_files << PostFile.new(
      filename: attributes['filename'],
      file: attributes['file']
    )
  end
end

我究竟做错了什么?

4

0 回答 0