Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to handle dynamic css in a rails app

I'm researching a problem for handling dynamic css in a rails app. Within the app, individual users and/or groups of users can have customized look and feel that is accomplished via CSS. There will not be any fixed number of "look and feels" or css files, the number will grow as the number of users and groups grows and the look and feel is defined by the users via the application's admin interface. Throughout the course of a typical day thousands (it not tens of thousands) of different variations of the css will be served up. The app will store the pre-built css in mongodb, so there it will not have to pay the price of constructing the css for every request, the question is more about how is the best way to serve up this dynamic css content. I've seen other questions like [this one][1] that speak to using erb or sass, but some of these answers are dated by several years so I wanted to make sure there wasn't a better answer with Rails 3.

like image 245
Chris Dellinger Avatar asked Jan 18 '11 21:01

Chris Dellinger


People also ask

Can you dynamically change CSS?

If you want to change the CSS styles dynamically you'll have to attach this portion of code to some event. For example, if you want to change the styles of your element when clicking on a button, then you have to first listen to the click event and attach a function containing the previous code.

Where do I put CSS in Ruby on Rails?

In the sea of different files created we can follow the path app/assets/stylesheets and create a new file style. css under the stylesheets folder. This file can be called anything but make sure you end it with the . css extension.

Can I use dynamic values in CSS?

CSS custom properties are a powerful and innovative way to bring more life to your stylesheets, introducing completely dynamic values for the first time in CSS.


2 Answers

You can treat your CSS files as resources, store them on the database, and serve them with page caching, so that you only need to hit the db once when the CSS is modified. All later requests will be served directly by the web server from the cache, without ever touching your app or db.

# stylesheet.rb
class Stylesheet < ActiveRecord::Base
  validates_presence_of :contents
end

# stylesheets_controller.rb
class StylesheetsController < ApplicationController
  caches_page :show # magic happens here

  def show
    @stylesheet = Stylesheet.find(params[:id])
    respond_to do |format|
      format.html # regular ERB template
      format.css { render :text => @stylesheet.contents, :content_type => "text/css" }
    end
  end
  # the rest is your typical RESTful controller, 
  # just remember to expire the cache when the stylesheet changes
end

# routes.rb
resources :stylesheets

# layouts/application.html.erb
…
<link href="<%= stylesheet_path(@current_user.stylesheet) %>" rel="stylesheet" type="text/css" />
like image 132
edgerunner Avatar answered Sep 20 '22 12:09

edgerunner


Well, I have worked with this a couple of times but they were definitely fixed no of CSS files to choose from. Its should be the same more or less.

One of things I used alot was the content_for blocks. Basically

<% content_for :css do %>
 // some css file or css content
<% end %>

And in the layout

<%=  yield :css %>
 

very simple way for managing the layouts.

like image 30
Rishav Rastogi Avatar answered Sep 21 '22 12:09

Rishav Rastogi