Reputation: 2960
I've just installed ActiveAdmin and created a form like:
form do |f|
f.inputs "Details" do
f.input :title
f.input :published_on, as: :datepicker
end
f.has_many :images do |images_f|
images_f.input :image, as: :file, hint: images_f.template.image_tag(images_f.object.image.url)
images_f.input :description, as: :text, input_html: { rows: 3 }
end
f.has_many :topics do |t_f|
t_f.input :maker
t_f.input :title
t_f.input :text
t_f.input :image, as: :file, hint: t_f.template.image_tag(t_f.object.image.url)
t_f.input :sort_order
end
f.buttons
end
I got quite a few mandatory feels so when I pick an image for within has_many :images and I click on save (knowing it will fail to validate), it shows me the image I picked in the hint which is fine. However, the image input is still shown and if I don't fill in any form and click submit again, it will again fail to validate but this time the image is gone.
Does anyone know how to prevent this? It's kinda annoying.
Upvotes: 1
Views: 1248
Reputation: 11929
There is no standart way for doing it, HTML file select controls are always initially blank in Rails and other frameworks,
and this is also enforced by most browsers. So that is not issue of activeadmin I think. This is because
web page could get you to upload an arbitrary file
by hiding the select from the user.
One of the custom options is to use hidden fields of uploaded files and add them if validation failed. So before validation starts you have to save files anywhere (tmp directory or somethong) and store links to them in virtual attribute of your model. After it you have to render hidden fields in your form with links to already uploaded files on your server. And instead if file inputs you can also display links to them with possibility to delete them.
Another option is to upload files with ajax from other form when you already have your model stored in db.
If you want files being mandatory for you model, you can mark you model as disabled or use some kind of temporary model , which is copy of your current one. After file become uploaded and model become valid, you can change it's state to enabled or copy temporary instance to another database table , depends on your choice.
Upvotes: 2