Best way to handle dynamic css in a rails app

前端 未结 5 913
攒了一身酷
攒了一身酷 2020-12-13 00:53

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 accomp

相关标签:
5条回答
  • 2020-12-13 01:24

    I had a similar problem - but needed to serve the modified CSS only once. I store a couple of constants in a module 'Site' - which I can then use as constants in CSS or as constants throughout the Rails application. I auto-generate the CSS files whenever the Rails application restarts and the CSS input files were modified.

    You could do something similar, but reference symbolic names in site_settings.rb and then fetch those on a per-user basis from MongoDB

    http://unixgods.org/~tilo/Ruby/Using_Variables_in_CSS_Files_with_Ruby_on_Rails.html

    0 讨论(0)
  • 2020-12-13 01:28

    Now let's say you have some dynamic styling called dynamic.css.scss.erb (the .erb at the end is important!) in app/assets/stylesheets. It will be processed by erb (and then by Sass), and as such can contain stuff like

    .some_container {
    <% favorite_tags do |tag, color| %>
    .tag.<%= tag %=> {
        background-color: #<%= color %>;
    }
    <% end %>
    

    }

    0 讨论(0)
  • 2020-12-13 01:33

    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" />
    
    0 讨论(0)
  • 2020-12-13 01:40

    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.

    0 讨论(0)
  • 2020-12-13 01:48

    This might give you a few ideas: Multiple robots.txt for subdomains in rails

    0 讨论(0)
提交回复
热议问题