Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have absolute path for stylesheets in mailer with the asset pipeline?

The view helpers in my Mailer template give me relative URLs to the stylesheet and images. Of course, this won't work if I'm viewing the email in Gmail, for example.

In apps/views/layouts/mailer.html.erb

<%= stylesheet_link_tag "application" %>
...
<%= link_to(image_tag("logo.png"), "http://mysite.com") %>

Renders as:

<link href="/assets/application-c90478153616a4165babd8cc6f4a28de.css" media="screen" rel="stylesheet" type="text/css" />
...
<a href="http://mysite.com"><img alt="Logo" src="/assets/logo-d3adbf8d0a7f7b6473e2130838635fed.png" /></a>

How do I get Rails to give me absolute links instead? I'm on Rails 3.1, the asset pipeline is in effect.

like image 599
rocketscientist Avatar asked Oct 18 '11 00:10

rocketscientist


People also ask

How do you Precompile an asset?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

What does rake assets Precompile do?

rake assets:precompile. We use rake assets:precompile to precompile our assets before pushing code to production. This command precompiles assets and places them under the public/assets directory in our Rails application.

How does mailer view differ from normal view in rails?

Mailers are really just another way to render a view. Instead of rendering a view and sending it over the HTTP protocol, they are sending it out through the email protocols instead. Due to this, it makes sense to have your controller tell the Mailer to send an email when a user is successfully created.


1 Answers

For rails 3.2 and ActionMailer use:

config.action_mailer.asset_host = "http://www.example.com"
like image 71
David Radcliffe Avatar answered Sep 30 '22 07:09

David Radcliffe