0

I'm trying to use carrierwave-base64 and accepts_nested_attributes_for to upload files but I get "File can't be blank" error. Here's an example of what I'm trying:

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

But if I do it like the code below, it works:

@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

What am I doing wrong?

0 Answers0