I'm using Paperclip to allow users to attach things, and then I'm sending an email and wanting to attach the file to the email. I'm trying to read the file in and add it as an attachment, like so:

# models/touchpoint_mailer.rb
class TouchpointMailer < ActionMailer::Base
  def notification_email(touchpoint)
    recipients "[email protected]"
    from "Touchpoint Customer Portal <[email protected]>"
    content_type "multipart/alternative"
    subject "New Touchpoint Request"
    body :touchpoint => touchpoint

    # Add any attachments the user has included
    touchpoint.assets.each do |asset|
      attachment :content_type => asset.file_content_type,
                 :body =>

This gives me the following error No such file or directory - /system/files/7/original/image.png?1254497688 with the stack trace saying it's the call to When I visit the show.html.erb page, and click on the link to the image, which is something like http://localhost:3000/system/files/7/original/image.png?1254497688, the image is displayed fine.

How can I fix this problem?

+1  A: 

Typically root_url should provide this. is expecting a file path, not a url though. If you are generating the images, you should call the image generating code and return the bytes of the generated image instead of calling…)

I'm not generating them, I'm letting the user upload them and storing them on the filesystem
Wayne M
But when I specify the filepath (via the asset.url method) I get an error saying it can't find the file; however when I link to it (e.g. link_to, asset.url) it works fine and I can view the image.
Wayne M
Try `, "public", asset.url))` (assuming the images are on the file system under `public/system/files/…`.
Hmm the problem seems to be the string of characters it adds after the filename, trying what cwninja said I get the error "Invalid argument". Is there any way to get the base path to the file without that gobbeldygook after it?
Wayne M
`asset.url.gsub(/\?.*/, "")` as a quick fix?
(Obviously you should be wrapping all this ick up in your model, `#data` method maybe?)
That works, but now I'm getting an Permission Denied error when I try to read the file, although Rails has permission to save the file to the folder.
Wayne M
The permission denied seems to be at the root directory (e.g. C:\Documents\Wayne\Projects\Portal\) but Rails is able to do everything else to that directory. Maybe it has to do with the fact this is via a mailer and not a controller?
Wayne M
mailer vs controller should not make a difference. As you are on windows (boooooo!), you may need to do a `, "public", *asset.url.gsub(/\?.*/, "").split("/"))`. It may be trying to read `C:\Documents\Wayne\Projects\Portal\public\system/files/7/original/image.png` which does not look like a valid windows (booooo!) file name.

asset.url returns the URL to the file. This is usually /system/classname/xx/xx/style/filename.ext. You'd put this in an image_tag.

You want asset.path. It returns the full path to the file, which will usually be something like /home/username/railsapp/public/system/classname/xx/xx/style/filename.ext