0

I'm using Rails 7 with Turbo. I'm trying to build a file upload mechanism - the idea is that user in the profile page could upload the file and after upload success, page should not be reloaded but it should show a green mark as a success confirmation.

My code:

# routes
kyc_document_upload POST   /kyc_document_upload(.:format) profile#kyc_document_upload

# profile_controller.rb
class ProfileController < ApplicationController
  def kyc_document_upload
    @file = FileRead.read(params[:file])
    body = Base64.encode64(@file)
    ExternalApi.user.sent_missing_docs(body)
  end

# views/profile/show.html.erb
<%= form_tag(kyc_document_upload_path, multipart: true) do %>
  <%= file_field_tag "file" %>
  <%= submit_tag %>
<% end %>

How to add turbo and green mark instead of redirection ?

I think it's not relevant but worth to mention that the file itself is not saved anywhere but is encoded into Base64 string and sends this file to an external service under the hood.

mr_muscle
  • 2,536
  • 18
  • 61
  • have you tried direct uploads ?? – Bouaik Lhoussaine Nov 02 '22 at 21:23
  • Direct uploads comes with Active Storage. I don't want to store any file. – mr_muscle Nov 02 '22 at 21:25
  • Basically if you submit your form, then you hit a route and either you redirect or display a view, the content of your page will change. Only option is sending your file aynchronously. If you include your form in a turbo frame, then it will send async and you can return a turbo stream. So basically you can leave the form untouched if you want. Other option is to catch the submit button, prevent default and do a fetch in JS, maybe through a Stimulus controller then modify your dom when an answer is received. so many ways to do this ... – Maxence Nov 03 '22 at 23:26

0 Answers0