免責聲明擔任新的主題和語言:我不熟悉戰爭的。我的答案可能完全錯誤,因爲我沒有經驗。請理解,我發現這是一個有趣的挑戰,我想找到一個解決方案。
如果當你創建一個WAR歸檔哪些應用程序能夠做到的事情,我不能告訴。結果我決定用命令行來做實際的編譯。但是,包含sass gem應該不太難,如果需要的話直接使用編譯器。
因爲我靠在命令行上,你必須安裝sass gem。然後使用命令行界面查看README。
爲了測試,我創建了一個名爲Theme
有以下的列模型。您可能需要更改下面的代碼以符合您的模型。
(string) title # the title of the theme
(string) stylesheet_file_name # the name of the file that is saved
在我的形式我用不同的字段名,以避免重疊。
<%= f.form_field :styleheet %>
然後在我的控制器中,我添加了代碼來編譯上傳的scss文件並將其移動到公共目錄中。
def create
if params[:theme].has_key?(:stylesheet)
# create a filename friendly version of the theme name
file_name = params[:theme][:stylesheet_file_name] = params[:theme][:title].parameterize
# where to copy the temporary uploaded file to. It is important that we get
# the original extension. The `sass` command uses the extension to determine how to compile the file
tmp_file = "#{Rails.root}/tmp/#{params[:theme][:stylesheet].original_filename}"
# move from /tmp path to within the Rails temp directory
`cp #{params[:theme][:stylesheet].tempfile.path} #{tmp_file}`
# create the theme's css file.
File.open("#{Rails.root}/public/stylesheets/#{file_name}.css", 'w') do |f|
# compile the .scss file via command line
parsed_theme = `sass #{tmp_file}`
# store the contents of the file
f.write(parsed_theme)
end
# remove the temporary file we created earlier
`rm #{tmp_file}`
# this key wasn't part of my AR model
params[:theme].delete("stylesheet")
end
# rest of action here ...
end
一旦你有了到位的CSS文件,你可以將其包含在你的layout/application.html.erb
文件有以下幾點。
<head>
<%= stylesheet_link_tag "/stylesheets/#{@current_theme.stylesheet_file_name}" %>
</head>
有趣,我會試試這個在星期一。 – Oleksi 2012-08-25 04:28:40