1

I am trying to attach a paperclip avatar image to an email, however it doesn't seem to realize it is an image that I am attaching, but when I place the url in my browser it finds the image. I am not sure if it is because of the random numbers that paperclip has at the end or not.

In my Usermailer

attachments.inline['avatar.jpg'] = File.read("localhost:3000#{@user.avatar.url(:medium)}")

In my email

<%= image_tag attachments['avatar.jpg'].url %>

The url it spits out is(The picture is a random stock photo for testing)

localhost:3000/system/users/avatars/000/000/026/medium/Maximus_Minimus_food_truck_Seattle_Washington.JPG?1397942965
Taylor Mitchell
  • 614
  • 11
  • 27
  • Is there a typo in your template statement `<%= image_tag attachments['avatar.jpg'].url -%>`. There is an extra dash in the closing %> statement. – cevaris Apr 19 '14 at 21:49
  • yep sorry. wasn't in my code. I should have copied and pasted. I must have accidentally hit it. – Taylor Mitchell Apr 19 '14 at 22:37

3 Answers3

1

You can use the Asset pipeline to attached the actual file path. Have not tested.

"#{Rails.root}/#{<YourAppName>::Application.assets.find_asset('avatar.jpg').pathname}"

You can find the answer here

Community
  • 1
  • 1
cevaris
  • 5,671
  • 2
  • 49
  • 34
  • The problem with this is I would need to know the actual filename. I don't know the filenames for people that upload their avatars. I am only saving the attachment as `avatar.jpg`. Not finding a file `avatar.jpg` – Taylor Mitchell Apr 20 '14 at 02:59
  • I had though `paperclip` renames the file? Or at least persists the path/location of it? May be wrong. – cevaris Apr 20 '14 at 15:55
  • no that url that it spits out `localhost:3000/system/users/avatars/000/000/026/medium/Maximus_Minimus_food_truck_Seattle_Washington.JPG?1397942965` – Taylor Mitchell Apr 21 '14 at 00:30
  • How about this `user.avatar.path`? [source](http://stackoverflow.com/questions/4984604/get-server-file-path-with-paperclip) – cevaris Apr 21 '14 at 05:32
1

Here's my model looks like

class Petition < ApplicationRecord
  has_attached_file :contract
end

In mailer, I attached this contract file like this. Hope this helps.

def send_invitation(petition)
  attachments[petition.contract_file_name] = File.read(petition.contract.path)
  mail(
    to:       @petition.email,
    subject: t('mailer.send_invitation')) do |format|
    format.html { render 'send_invitation' }
end
Mayank
  • 171
  • 1
  • 6
0

I think you are supposed to use

attachments.inline['avatar.jpg'] = @user.avatar.data

Matthias
  • 21
  • 3