从 Rails 应用程序的数据库中提取 CSS 资产(如背景图像)? [英] Pulling CSS assets (like background images) from the database in a Rails app?

查看:28
本文介绍了从 Rails 应用程序的数据库中提取 CSS 资产(如背景图像)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以将 CSS 资产路径(如背景图像)存储在数据库中,以便无需访问或重写代码即可对其进行自定义.我看过一些模板引擎,喜欢液体,但认为它们对我想要的东西来说太过分了去做.我只希望在同一应用程序代码库的各种部署之间的视图中进行一点点自定义,而不是针对各种用户的任何内容.

I'm wondering if there's a way to store CSS asset paths, like background images, in the database so that they're customizable without accessing or rewriting the code. I've looked at some template engines, like liquid, but think they're a overkill for what I want to do. I only want a tiny bit of customization in my views between various deployments of the same app codebase, not anything for various users.

我对 Rails 3.1 的研究不多,但据我所知,CSS 资产是经过编译的并且不再是静态的,所以——这是否意味着我可以在 Rails 3.1 中将类似的内容写入我的 CSS 中从数据库中提取?我通常部署到 Heroku,但不确定他们是否支持 3.1.

I've not looked much at Rails 3.1, but from what I understand the CSS assets are compiled and aren't static any longer, so -- does that mean I can write something like that into my CSS in rails 3.1 that pulls from the database? I usually deploy to Heroku and aren't sure if they're supporting 3.1 yet.

有人有更好的策略或想法吗?

Anyone have any better strategies or ideas?

推荐答案

在数据库中存储像图像这样的二进制文件很快就会变得很麻烦.曾经有一段时间我们将用户上传的文件(如 PDF 等)存储在数据库中,但它变得无法管理.我们迅速将其全部转移到 S3,并让 Paperclip 在那里存储和检索文件(在将文件保存到 S3 之前对文件进行加密,并通过 SSL 发送它们,因为这些文件可能是敏感的),这让事情变得更加理智.

Storing binary files like images in the database can quickly become cumbersome. There was a time when we were storing user uploads like PDF's and such in the DB but it became unmanageable. We quickly moved it all over to S3 and made Paperclip store and retrieve the files there (encrypting the files before saving them to S3, and sending them over SSL since the files were potentially sensitive) and it made things much saner.

我认为对您来说最好的选择是使用 S3.由于

I'd say best bet for you is to use S3. Since

  1. 在 heroku 上你有一个有限的数据库大小(取决于您的计划)并且可能很快用完,如果您正在那里存储二进制文件.
  2. 您无法动态保存新文件到 heroku 上的文件系统,和
  3. S3 便宜得要命(而且免费最随意的用途)来存储和检索文件.

根据您的评论进行

好的,我误解了这个问题.要么将图像路径存储在数据库中,要么将图像存储在这样的路径中 &代码本身可以确定从哪里获取图像的命名约定(这就是回形针所做的).两种方式都可以接受.请注意,SASS 并不是真正动态的,您无法从数据库中提取路径并即时更改 sass.我遇到了类似的情况,解决方案是让 CSS 指向一个背景图像,它实际上是应用程序中的一条路径.在我们的实例中,我们能够根据传入用户的子域或域更改显示的图像,但您也可以根据在呈现视图之前设置的会话 cookie 轻松显示该图像.

Ok I mis-understood the question. Either store the image path in the database or have the image stored with such a path & naming convention that the code itself can figure out where to get the image (which is what paperclip does). Both ways are acceptable. NOTE that SASS is not truly dynamic, you can't pull paths from the database and make the sass change on-the-fly. I've run into a similar situation and the solution was to make the CSS point to a background-image that was in fact a route in the application. In our instance we were able to change the image displayed based on the subdomain or domain of the incoming user, but you could just as easily display that image based on a session cookie that gets set before the views are rendered.

在编译 SASS 时,生成后它是静态的.它的语法和动态"性质只是为了使编写 CSS 更容易.

While SASS is compiled, after it's been generated it is static. The syntax, and 'dynamic' nature of it are just to make writing CSS easier.

这篇关于从 Rails 应用程序的数据库中提取 CSS 资产(如背景图像)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆