gpt4 book ai didi

ruby-on-rails - 使用 rails Assets 管道时如何内联 css

转载 作者:行者123 更新时间:2023-12-03 10:44:13 26 4
gpt4 key购买 nike

而不是让页面包含一个带有链接的样式标签,从哪里获取 css,我可以使用 rails 的 stylesheet_link_tag 添加到我的 View 中。辅助方法,我想直接在页面内嵌入 css。

这是我到目前为止想出的:

%style(type="text/css")=File.read(physical_asset_path("email.css"))

但是我找不到任何可以为我提供 Assets 物理路径的 Rails 辅助方法 - physical_asset_path只是我发明的一种虚拟方法。

有人知道如何在使用 rails 3.2.x 时获取 Assets 的物理路径吗?

有没有更简单/更好的方法来获取样式表 - 从共轨 Assets 路径内的 css 文件 - 内联?

用例:大多数电子邮件客户端在未经用户确认的情况下不会访问外部资源(如 css、图像)。因此,为了正确显示电子邮件,我需要将 CSS 嵌入电子邮件的 HTML 中。

最佳答案

使用 premailer 或 premailer-rails3

https://github.com/fphilipe/premailer-rails3
或者
https://github.com/alexdunae/premailer

乔的 Nerd 派对 说:

We also used the Premailer gem to automatically inline the linked stylesheet in the email views. Our email layout looks something like:


%html
%head
= stylesheet_link_tag 'email'

%style{:type => "text/css"}
:sass
@media all and (max-width: 480px)
table#container
width: auto !important
max-width: 600px !important
... and so on for the mobile code

%body
Email body here.
%table
Lots of tables.

We include a stylesheet in the HTML. Premailer downloads it, processes it, and inserts the css rules inline in the HTML.

The @media rules need to be inline in the email layout, since Premailer can’t handle those being in a separate css file yet.

We use premailer-rails3 to integrate Premailer into Rails 3. Unfortunately, we found a bunch of bugs in premailer and premailer-rails3. Our forks of the projects are at https://github.com/joevandyk/premailer and https://github.com/joevandyk/premailer-rails3. The forks fix some encoding bugs, remove some weird css processing stuff done by premailer-rails3, allow premailer to not strip out embedded rules in the email layouts, and some other things.

We also found a bug in sass-rails, where you can’t embed image-urls in inline sass code. See https://github.com/rails/sass-rails/issues/71 Premailer-rails3 hooks into ActionMailer when the email actually being delivered, not just generated. When running tests, email is not actually sent, so the premailer-rails3 hooks don’t get ran during tests. I haven’t spent the time to see if it’s possible to get the premailer processing to run during tests, but that would be a nice thing to do.

Also, our forks on premailer-rails3 assume that you want premailer to go out and actually download the linked CSS files. It should be possible to use the Rails 3.1 asset pipeline to get the processed css without downloading it. A very special thanks goes to Jordan Isip who did the super annoying job of making sure the emails look great in all the different clients out there. Writing that CSS/HTML did not look fun.



更新:

Roadie似乎是一个更好的选择。感谢赛斯兄弟指出。

关于ruby-on-rails - 使用 rails Assets 管道时如何内联 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10386923/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com