A
A
aaltqna2022-03-28 13:51:23
Ruby on Rails
aaltqna, 2022-03-28 13:51:23

How to attach a newly created excel file to a post?

There is a mailer in which an excel file is generated and attached to the mailing list, everything is like here:
https://github.com/caxlsx/caxlsx_rails#mailers

xlsx = render_to_string layout: false, template: "my_dir/export"
xlsx_base64 = Base64.encode64(xlsx)
attachment = {mime_type: Mime[:xlsx], content: xlsx_base64, encoding: 'base64'}
attachments["file.xlsx"] = attachment


I need every file sent to be saved. To do this, I added a new model with has_one_attached:
class MyLog < ApplicationRecord
  has_one_attached :xlsx_file
end


And after creating the file, I want to create a new entry with this attached file:
MyLog.create(
  # xlsx - это то, что я выше создал. Возможно нужен xlsx_base64 или attachment
  xlsx_file: xlsx
)


The problem is that caxls creates a file and returns a string. This is according to their instructions for mailing attachments. For example, in the active_storage_attachment documentation, a file_field is used to get a file, and a file with the ActionDispatch::Http::UploadedFile class comes from it. If it is passed to xlsx_file, then everything works fine.

But I do not need to receive the file through this tag, but take the newly created one and attach it. How can this be done at all?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question