forest-rails
forest-rails copied to clipboard
Picture uploads with Active Storage [help wanted]
I would like to setup image uploads with Active Storage. However, I am not sure Forest supports it.
On my app I have a Product model which:
- has_one_attached :logo
- has_many_attached :screenshots
Could you advise me on the cleanest way to do so? It feel like building a smart action for it would be counter-intuitive. Couldn't find any documentation on it.
Thanks a lot.
Is there any information on this issue? I cannot use the admin panel just because there are no instructions for uploading files with ActiveStorage (((
Hi @JulienTimsch @mcmegavolt, indeed the experience is not good at all with the default liana behaviour. I'll provide a configuration example to have a better experience.
Display implementation example
Here is a simple configuration on a User
model:
# app/models/users.rb
class User < ApplicationRecord
has_one_attached :avatar
end
The liana won't natively send the picture url to the admin panel, so you have to create a Smart Field for this purpose:
# lib/forest_liana/collections/user.rb
class Forest::User
include ForestLiana::Collection
collection :User
field :avatar, type: 'String' do
Rails.application.routes.url_helpers.rails_blob_path(object.avatar, only_path: true)
end
end
(See more about Smart Field creation here: https://docs.forestadmin.com/documentation/reference-guide/fields/create-and-manage-smart-fields#creating-a-smart-field)
That should be pretty much it for the code. Now the Smart Field values should be appear in the admin panel.
Then you can configure the display of the field adding the image viewer
widget on the Smart Field:

Eventually, the avatar should have a nice display:

@arnaudbesnier Many thanks! Will try your solution shortly. But what about has_many_attached :screenshots association? Is it possible to setup multi image uploading?
Hi @mcmegavolt I have the same issue than you... Did you find a solution for the has_many_attached ? If yes, I'd love to get it.
Hi, Thanks @arnaudbesnier for your implem proposition ! I managed to view the image.
But I couldn't figure out how to upload a file. Is this feauture available somehow ?
I disabled readonly flag of course.
On the edit settings I chose File Picker. May be the issue is with the prefix I use on the edit settings panel for the field ?
Has anybody manage to upload files, not view them only ?
Thanks
Little bump on this, did you guys find a way to upload a file in the end ? @iwakidi @meryldelpech @arnaudbesnier
Still looking for it. ActiveStorage is an out of the box solution for rails and is widely used. I don't get how this is not out of the box for Forest
Unfortunately we don’t have a solution at the moment. However we are working on an evolution of this stack and it’s planned that ‘active_storage’ will be one of the features available.
hello @arnaudbesnier , are you still working on a solution about active storage and forest liana ?
I also want to create a record via ForestAdmin but need to create attached image with active storage.
Any solution ?
If not possible directly with a smart field.
Do you have a solution about a SmartAction that would do the job of creating a record and also attached an uploaded pictore to the record before completed the task ?
A correct handling of Active Storage would be extremely appropriate, since it is now the default file handling solution for Rails.
What is very unfortunate is that because of the way smart field updates works (with a hash instead of the ORM object), it cannot even be solved with a smart field! A smart action needs to be used, which is very bad for our ops workflows.
In order of preference, preferred scenariis would be:
-
out of the box support just like Carrierwave and Paperclip : attachments appear as a field of the model and can be directly viewed as a file and updated
-
ability to manipulate the object instead of a hash in a smart field setter, which would allow us to do this workaround which is not too bad:
set_file = lambda do |bill, file|
bill.attach(
io: StringIO.new(Base64.decode64(file)),
filename: "bill.pdf",
content_type: "application/pdf"
)
bill
end
field :file, type: "String", set: set_file, is_read_only: false do
object.bill.url
end
Right now the setter cannot work because a hash has to manipulated and returned.
- What we have, ugly SmartActions for every attachment
For such an expensive service, I would expect something that works better with Rails default functionality 🙏