Temporary carrier wave file without model

I need to be able to attach a file to mail (using Mailer) for a recently downloaded file that is not associated with any model.

In the code that appears 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'm looking at in the send_mail action is something like:

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

with parameters [: mail] [: attachment] - the path to the temp file loaded using the form. How can I do that?

This also implies another question: should I manually delete the file from temp after sending mail? If so, how?

+5
source share
2 answers

Copy the answer from the comments to remove this question from the "No Answer" filter:

Finally, he 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

+5

, , , CarrierWave . file_field Rails Tempfile, :

params[:mail][:attachment].path

, , Rails docs :

- , - . , Ruby , .

0

All Articles