I need to save a PDF using wicked_pdf gem and Active Storage, unfortunately I haven't achieved it.
It is a very simple scenario, I need to create the PDF and save the object in the same process. The PDF is generated from a view.
@user = User.new(user_params)
view_string = render_to_string("user_pdf_view")
pdf = WickedPdf.new.pdf_from_string(view_string)
io_pdf = StringIO.new(pdf)
@user.pdf.attach(io: io_pdf,
filename: "user_filename",
content_type: 'application/pdf')
If the object isn't persisted, I get this error:
*** NameError Exception: uninitialized constant #<Class:0x00007ff6b30b3c98>::Analyzable
Did you mean? ActiveStorage::Analyzer
And if the object is already saved, I get this one:
*** NoMethodError Exception: undefined method `build_after_unfurling' for #<Class:0x00007ff6b30b3c98>
enter code here
According to active storage docs, it is possible to attach an IO Object. I've also tried using a File and Tempfile instead of StringIO but with the same outcome.
I'm going through AS source code to see what's going on, but it would be great if someone has gone through this before and can help me up.
Thanks!
EDIT:
Update: I've just realized that if I change the config.eager_load to true... the error doesn't happen anymore.