在rails应用程序中处理动态css的最佳方式 [英] Best way to handle dynamic css in a rails app

查看:92
本文介绍了在rails应用程序中处理动态css的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究在rails应用程序中处理动态css的问题。在应用程序中,个人用户和/或用户组可以具有通过CSS实现的自定义外观和感觉。不会有任何固定数量的外观和感觉或CSS文件,数量将随着用户和组的数量的增长而增长,外观和感觉由用户通过应用程序的管理界面定义。在整个过程中,一个典型的日子,数千(不是数万)的不同变化的CSS将被提供。该应用程序将存储预建css在mongodb,所以它不必为每一个请求构建css的价格,问题是更多的是如何提供这种动态css内容的最佳方式。我看过其他问题,如[这一个] [1]说使用erb或sass,但其中一些答案的日期几年,所以我想确保没有一个更好的答案与Rails 3。 / p>

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.

推荐答案

您可以将CSS文件视为资源,将其存储在数据库中, 网页缓存 ,以便您只需要点击db 一次。稍后的所有请求都将由网络服务器直接从缓存提供,而不会触及您的应用或数据库。

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" />

这篇关于在rails应用程序中处理动态css的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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