asset_url in mailer on rails 3.1
Asked Answered
M

1

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 ?

Mcclendon answered 21/10, 2011 at 19:18 Comment(3)
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.Morey
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 emailMcclendon
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!Morey
W
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.

Wichman answered 12/1, 2012 at 13:28 Comment(1)
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. thanksLegnica

© 2022 - 2024 — McMap. All rights reserved.