forest-rails icon indicating copy to clipboard operation
forest-rails copied to clipboard

Picture uploads with Active Storage [help wanted]

Open JulienTimsch opened this issue 6 years ago • 12 comments

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.

JulienTimsch avatar Feb 04 '19 17:02 JulienTimsch

Is there any information on this issue? I cannot use the admin panel just because there are no instructions for uploading files with ActiveStorage (((

mcmegavolt avatar Feb 17 '19 14:02 mcmegavolt

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.

arnaudbesnier avatar Apr 18 '19 16:04 arnaudbesnier

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:

Screenshot 2019-04-18 at 19 08 14

Eventually, the avatar should have a nice display:

Screenshot 2019-04-18 at 19 07 34

arnaudbesnier avatar Apr 18 '19 17:04 arnaudbesnier

@arnaudbesnier Many thanks! Will try your solution shortly. But what about has_many_attached :screenshots association? Is it possible to setup multi image uploading?

mcmegavolt avatar May 17 '19 22:05 mcmegavolt

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.

meryldelpech avatar Aug 18 '20 14:08 meryldelpech

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

iwakidi avatar Apr 24 '21 11:04 iwakidi

Little bump on this, did you guys find a way to upload a file in the end ? @iwakidi @meryldelpech @arnaudbesnier

albandum avatar Jun 29 '21 18:06 albandum

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

daniel-gato avatar Mar 30 '23 15:03 daniel-gato

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.

matthv avatar Apr 03 '23 16:04 matthv

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 ?

elviajero971 avatar May 19 '23 12:05 elviajero971

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:

  1. 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

  2. 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.

  1. What we have, ugly SmartActions for every attachment

For such an expensive service, I would expect something that works better with Rails default functionality 🙏

natoromano avatar Jun 21 '23 10:06 natoromano