Get file and filename from carrierwave

I successfully uploaded a file to S3 and now I want to attach file to email, so i can send it by using Gmail API. Some people suggested to call method .file directly like here

as you can see, you can call method

<%= @page.form.file.filename %>

.file directly from form.

here is my mount uploader class:

class Crm::LogEmailAttachment < Asset
    belongs_to :owner, class_name: 'Crm::LogEmail'
    mount_uploader :data, Crm::LogEmailAttachmentUploader
    mount_base64_uploader :data, Crm::LogEmailAttachmentUploader
    # default_scope -> {order("assets.order")}

    def to_s
      self.data.try(:file).try(:filename)
    end
end

and here is my erb form:

<div class="document-wrap">
   <%= log_email.fields_for :crm_log_email_attachments do |a| %>
    <%= a. file_field :data, input_html: {class: "form-control" , wrapper: 'field'}, label: false %>
   <% end %>
</div>

then I try to get file by just calling .file from params[:data] it returns undefined method file for #<ActionDispatch::Http::UploadedFile:0x007fc337322290>, my question is how can I get the path of the file and filename from carrierwave ?

Upvotes: 0

Views: 3798

Answers (1)

Anthony L
Anthony L

Reputation: 2169

Instead of referencing the params ActionDispatch::Http::UploadedFile object, you should save the data to S3 via carrierwave first, then reference the uploader object directly. For instance

object.data.file.filename

To retrieve the filename, and

object.data.url

To retrieve the S3 URL for the associated asset.

Upvotes: 3

Related Questions