0

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.

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?

Slick23
  • 5,827
  • 10
  • 41
  • 72

2 Answers2

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.

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

  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. You can't dynamically save new files to the filesystem on heroku, and
  3. S3 is cheap as hell (and free for most casual use) to store and retrieve files.

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.

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.

nzifnab
  • 15,876
  • 3
  • 50
  • 65
  • I should've been clearer ... I meant storing the image path in the database so it could be pulled into the CSS dynamically. Not storing the image itself in the database. – Slick23 Jun 16 '11 at 02:54
  • @Final I added some additional comments based on this information :p Sorry for misunderstanding! – nzifnab Jun 16 '11 at 16:21
1

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

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.

Msencenb
  • 5,675
  • 11
  • 52
  • 84
  • Would you then have to include the CSS in the actual template file, or can you use instance variables in a stylesheet for the view? – Slick23 Jun 16 '11 at 02:55
  • Well since you are just storing image paths (and not the actual images) you could pull that from the database store it in an instance variable and then use jQuery in the view + that instance variable to set the background-image CSS property. – Msencenb Jun 16 '11 at 05:17