Question

I want to send an email to a user with a link to download a PDF file in my public folder. How do I create the link from within an ActionMailer view? I need a solution that will work in my Test, Development, and Production environments (so, no hard coded links).

Was it helpful?

Solution

If you already have a route that you use in a normal view, then you can just replace _path with _url.

If you are building a custom link, then just use the root_url variable in a string which will print out: http://yourhost.com/

eg for HTML emails, assuming a filename variable

<%= link_to 'Download PDF', "#{root_url}#{filename}" %>

for Text emails:

<%= "#{root_url}#{filename}" %>

Make sure you set your correct host name for each environment in your config/environments/ENV.rb as this is where the _url gets its hostname from

eg: config/environments/production.rb

config.action_mailer.default_url_options = { :host => 'yourhost.com' }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top