0

I'm quite new with Rails and I'm trying to implement a multistep form in my app for creating a new instance of my User model (devise gem).

I use wicked gem and I've chosen a cache persistence and an ID/Key-in-Session as strategies to handle it.

For now, everything works well until I come across the avatar step in my form which is my last one (a @user has_one_attached :avatar).

When I try to submit my form, I got the below issue raising from that line: Rails.cache.write(session.id, user_attrs)

TypeError in Users::UserStepsController#update
can't dump File

Here my user_attrs when I submit my picture:

{"messenger_id"=>"1234567890123456",
"form_step"=>:profile_picture_step,
"first_name"=>"John",
"last_name"=>"Doe",
"email"=>"john@gmail.com",
"password"=>"1234",
"password_confirmation"=>"1234",
"avatar"=>#<ActionDispatch::Http::UploadedFile:0x00007fbdc4cdf418
  @tempfile=#<Tempfile:/var/folders/m_/8ln6gczs1ns1gnn53p6vskwr0000gn/T/RackMultipart20210527-5416-uy59kl.png>,
  @original_filename="Screenshot 2021-05-26 at 20.34.43.png", @content_type="image/png",
  @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"Screenshot 2021-05-26 at 20.34.43.png\"\r\nContent-Type: image/png\r\n">}

It seems Rails.cache.write is not able to write this avatar object, am I right? If so, is there a way to fix it?

Thank you for your help!

raphael-allard
  • 205
  • 2
  • 9
  • I think you want to read the file and save it's contents (are you sure you want to save that into your cache? if its memory cache it can get bloated pretty fast). https://stackoverflow.com/questions/2521053/how-to-read-a-user-uploaded-file-without-saving-it-to-the-database – Joel Blum May 27 '21 at 13:09
  • Thanks for your answer @JoelBlum! You must be right, I will investigate it. – raphael-allard May 27 '21 at 15:03
  • 1
    ActiveStorage can do direct uploads from the client straight up to a cloud service such as S3. You just add the JS to assets/packs and then add `direct_upload: true ` to the input: `<%= form.file_field :avatar, direct_upload: true %>`. https://guides.rubyonrails.org/active_storage_overview.html#direct-uploads – max May 28 '21 at 00:09
  • Sorry for this late answer, I will have a look on this resource. Thanks max – raphael-allard May 31 '21 at 15:35

0 Answers0