Instead of having the page include a style tag with a link where to get the css from, which I could add to my view using rails' stylesheet_link_tag
helper method, I want to have the css inline directly inside the page.
This is what I came up with so far:
%style(type="text/css")=File.read(physical_asset_path("email.css"))
But I can't find any rails' helper method which gives me the physical path of an asset - physical_asset_path
is just a dummy method invented by me.
Anybody knows how to get the physical path of an asset when using rails 3.2.x?
Is there an easier/ better way to get stylesheets - from css files inside the common rails assets paths - inline?
Use case: most email clients don't access external sources (like css, images) without user confirmation. So to get the emails properly displayed I need to embed the CSS inside the emails' HTML.
Use premailer or premailer-rails3
https://github.com/fphilipe/premailer-rails3 or https://github.com/alexdunae/premailer
Joe's Nerd Party say:
Update:
Roadie appears to be a better option. Thanks to Seth Bro for pointing it out.
Had the same problem, solved it using @phlegx's answer to a similar issue in Premailer.
For an environment-safe solution you need to use
I've packaged it into a helper in my app:
tl;dr (without Roadie):
For actually applying the CSS as inline styles, I recommend roadie-rails (which is a Rails wrapper for Roadie). It also has other neat features like absolutizing
href
s,src
s etc.A usage combining both inlined (
email.scss
) and non-inlined (email_responsive.css
) stylesheets, both residing inapp/assets/stylesheets
:Rails.application.assets.find_asset('email').to_s
will return the compiled asset as a string.Rails.application.assets['asset.js']
will work only in local environment, as rails asset compilation is disabled in both production and staging environment.Rails.application.assets_manifest.find_sources('asset.js').first.to_s.html_safe
should be used to inline css when using rails asset pipeline.You can use this: