在Rails应用程序中从数据库中提取CSS资源(如背景图像)?

时间:2021-05-14 20:41:49

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.

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

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.

我不太关注Rails 3.1,但据我所知,CSS资产已被编译并且不再是静态的,所以 - 这是否意味着我可以将这样的东西写入我的CSS中的rails 3.1数据库?我通常部署到Heroku,并不确定他们是否支持3.1。

Anyone have any better strategies or ideas?

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

2 个解决方案

#1


2  

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.

在数据库中存储像图像这样的二进制文件很快就会变得很麻烦。有一段时间我们在数据库中存储像PDF等用户上传,但它变得无法管理。我们很快将它全部移动到S3并使Paperclip存储并检索那里的文件(在将文件保存到S3之前加密文件,并通过SSL发送它们,因为文件可能是敏感的)并且它使事情更加精明。

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

我敢说最好的选择是使用S3。以来

  1. On heroku you have a limited database size (depending on your plan) and could quickly run out if you're storing binaries there.
  2. 在heroku上,您拥有有限的数据库大小(取决于您的计划),如果您在那里存储二进制文件,可能会很快耗尽。

  3. You can't dynamically save new files to the filesystem on heroku, and
  4. 您无法将新文件动态保存到heroku上的文件系统中

  5. S3 is cheap as hell (and free for most casual use) to store and retrieve files.
  6. S3对于存储和检索文件而言很便宜(并且对于大多数临时使用而言是免费的)。

EDIT based on your comment:

根据您的评论编辑:

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指向一个背景图像,这实际上是应用程序中的一个路径。在我们的实例中,我们能够根据传入用户的子域或域更改显示的图像,但您可以根据在呈现视图之前设置的会话cookie轻松显示该图像。

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.

在编译SASS时,在生成它之后它是静态的。它的语法和“动态”特性只是为了简化CSS编写。

#2


1  

What about using something like S3(Paperclip) + a "css assets" table/model?

那么使用像S3(Paperclip)+“css assets”表/模型这样的东西呢?

That way in an "admin" page you can pull all of the possible CSS assets, allow someone to select a new one or even upload a new image to s3. This means you wouldn't have to actually rewrite any code just have an admin portal where they can select possible images.

这样,在“管理”页面中,您可以提取所有可能的CSS资产,允许某人选择新资产,甚至将新图像上传到s3。这意味着您不必实际重写任何代码,只需拥有一个管理门户,他们可以选择可能的图像。

#1


2  

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.

在数据库中存储像图像这样的二进制文件很快就会变得很麻烦。有一段时间我们在数据库中存储像PDF等用户上传,但它变得无法管理。我们很快将它全部移动到S3并使Paperclip存储并检索那里的文件(在将文件保存到S3之前加密文件,并通过SSL发送它们,因为文件可能是敏感的)并且它使事情更加精明。

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

我敢说最好的选择是使用S3。以来

  1. On heroku you have a limited database size (depending on your plan) and could quickly run out if you're storing binaries there.
  2. 在heroku上,您拥有有限的数据库大小(取决于您的计划),如果您在那里存储二进制文件,可能会很快耗尽。

  3. You can't dynamically save new files to the filesystem on heroku, and
  4. 您无法将新文件动态保存到heroku上的文件系统中

  5. S3 is cheap as hell (and free for most casual use) to store and retrieve files.
  6. S3对于存储和检索文件而言很便宜(并且对于大多数临时使用而言是免费的)。

EDIT based on your comment:

根据您的评论编辑:

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指向一个背景图像,这实际上是应用程序中的一个路径。在我们的实例中,我们能够根据传入用户的子域或域更改显示的图像,但您可以根据在呈现视图之前设置的会话cookie轻松显示该图像。

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.

在编译SASS时,在生成它之后它是静态的。它的语法和“动态”特性只是为了简化CSS编写。

#2


1  

What about using something like S3(Paperclip) + a "css assets" table/model?

那么使用像S3(Paperclip)+“css assets”表/模型这样的东西呢?

That way in an "admin" page you can pull all of the possible CSS assets, allow someone to select a new one or even upload a new image to s3. This means you wouldn't have to actually rewrite any code just have an admin portal where they can select possible images.

这样,在“管理”页面中,您可以提取所有可能的CSS资产,允许某人选择新资产,甚至将新图像上传到s3。这意味着您不必实际重写任何代码,只需拥有一个管理门户,他们可以选择可能的图像。