Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carrierwave Temporary File Without Model

I need to be able to attach a file to a mail (using Mailer) for a recently uploaded file which is not linked to any Model.

In the code that goes for the upload form:

<%= form_for(:mail, :url => {:action => 'send_mail'}, :html => {:multipart => true}) do |f| %>
<table summary="send_table">
  <tr>
    <th>Attachment</th>
    <td><%= f.file_field(:attachment) %><a id="attachment"></a></td>
  </tr>
</table>

<%= submit_tag "Send!" %>

Now, what I am looking into doing in the send_mail action is something like:

MyMailer.send_mail(params[:mail][:attachment]).deliver 

with params[:mail][:attachment] being the path to the temp file uploaded with the form. How can one do that?

This also implies another question: Should I manually delete the file from the temp once the mail is sent? If yes, how?

like image 601
muichkine Avatar asked Feb 20 '23 05:02

muichkine


1 Answers

Copying the answer from the comments in order to remove this question from the "Unanswered" filter:

Finally nailed it: 

unless (params[:mail][:attachment]).nil?
  uploader = AttachmentUploader.new
  uploader.cache!(params[:mail][:attachment])
  @file_name = uploader.filename
  @file_data = uploader.file.read()
end

and then

MyMailer.send_mail(@file_name,@file_data)

~ answer per user1563325

like image 158
DreadPirateShawn Avatar answered Feb 27 '23 22:02

DreadPirateShawn