0

I have two models

class ComfortFactorSubCategory < ApplicationRecord
   has_one  :image, as: :imageable, dependent: :destroy
   validates :heading, presence: true
   accepts_nested_attributes_for :image, :reject_if => lambda { |a| a[:name].blank? }, allow_destroy: true
end

class Image < ApplicationRecord
  belongs_to :imageable, polymorphic: true
  mount_uploader :name, IconUploader
end

and my admin/comfort_factor_sub_category.rb I have these lines

ActiveAdmin.register ComfortFactorSubCategory do
  permit_params do
    permitted = [:heading, image_attributes: [:name, :_destroy, :id], additional_instruction_ids: []]
  permitted
  end
  form do |f|
    f.inputs do
      f.input :heading
      f.input :additional_instructions, as: :select, collection: AdditionalInstruction.pluck(:description, :id)
      f.fields_for :image do |b|
        b.input :name, label: "Image", :as => :file
      end
    end
    f.actions
  end
end

when I submit the form with wrong information lets say without heading and validation fail why do I need to select the image again while I did select in first submission

Asnad Atta
  • 3,855
  • 1
  • 32
  • 49

2 Answers2

1

Seems like you need f.hidden_filed :name_cache for caching uploaded file

Take a look at the CarrierWave making uploads work across form redisplays section and upload through accept_nested_attributes_for wiki article

Peter Balaban
  • 653
  • 3
  • 7
0

This is constrained by browser for security reasons. It avoids pre-filling the form with the default file object's value when error page is rendered.

This is a detailed excerpt about this.

This however is usually not supported by browsers. The usual explanation is “security reasons.” And indeed it would be a security risk if files from the user’s disk were submitted without the user’s content. It might be all too easy to lure some users into submitting some password files!

I also found this another answer talking about the same problem with ASP.net framework.

kiddorails
  • 12,961
  • 2
  • 32
  • 41