9

I have my mailer on rails 3.1 which has an inline attachment.

To open that attachment i use this code:

attachments["rails.png"] = File.read("#{Rails.root}/app/assets/images/Rails.png")

is there a way to change that with something like assets_url ?

Nicos Karalis
  • 3,724
  • 4
  • 33
  • 62
  • You don't need (nor want) to use something like asset_url for this... It would only save you first few words. Also, shorter version: `File.read(Rails.root.join('app/assets/images', 'Rails.png'))` from which you can make your own "asset_url"-like helper. – Dalibor Filus Dec 09 '11 at 22:04
  • but i can put my assets in the vendor assets file, like a external js file, or an external image that i want to be added to the body of the email – Nicos Karalis Dec 10 '11 at 01:54
  • Can you do something like `File.read(Rails.root.join('public', view_context.asset_path('Rails.png')))` ? It should work as compiled assets are always in public/assets. That `view_context` may not be necessary (or it may not be available in ActionMailer :( ) but let's give it a try! – Dalibor Filus Dec 10 '11 at 16:11

1 Answers1

10

If I understand correctly, you want to use the asset pipeline's search functionality to locate the local path for a given asset so you don't have to hard-code which directory it's in. If that's the case, you want to do this:

<YourAppName>::Application.assets.find_asset('Rails.png').pathname

This will locate the asset using standard pipeline/sprockets searching, and give you the fully qualified local path to the file.

Irongaze.com
  • 1,639
  • 16
  • 22
  • hi, this gives me the path in 'app/assets/..." but doesn't give the actual asset path in 'public/assets'. it's cool to know this though, but even in production environment it doesn't give me the directory i wanted. good to know though i've added to my notes. thanks – FireDragon Feb 19 '14 at 06:41