3

I had an app based on Rails 3.0. This is a prediction app and I need different CSS rule sets on different spreads, because I use same selectors with different rules.

Previously in rails 3.0 I had a nice solution.

  1. I had a column in my database at my spreads with the name of the CSS file (without extension).

  2. I picked up this data like this:

    @spread = Spread.find_by_id(params[:spread_id])
    @css_to_use = @spread.css_to_use
    
  3. Put a conditional line in my application.html.erb:

    <%= stylesheet_link_tag @css_to_use unless @css_to_use.blank? %>
    

This worked well, untill now. I'm working this app out for Rails 3.2 and because of the asset pipeline, this magic is gone.

I found this: Using Rails 3.1 assets pipeline to conditionally use certain css, but this is a bit sluggish solution (and now exactly what I want).

Is out there a nice work around for this problem? Do you know a solution that makes not only to load specified files but with a dependency?

Community
  • 1
  • 1
Kael
  • 565
  • 2
  • 8
  • 18

1 Answers1

3

i have a project with similar requirements and i use the techniques shown in the linked answer:

# app/views/layouts/application.html.haml
= stylesheet_link_tag "application", "labels/#{Whitelabel[:label_id]}"

# config/application.rb
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
config.assets.precompile += %w( active_admin.js active_admin.css labels/* )

this includes an additional stylesheet, that is not included in the application.rb

have a look at the full source: https://github.com/phoet/on_ruby/

phoet
  • 18,688
  • 4
  • 46
  • 74
  • As I understand this solution handles only one CSS file's separation. This is unable to handle a dynamically handled CSS load. Or do I miss something? – Kael Jul 31 '12 at 14:42
  • no, you could put arbitrary files into some folders and load them based on a key in the database. that is what i thought you were trying to do. maybe you need to clarify what exactly you want to achieve... – phoet Jul 31 '12 at 17:58
  • 1
    You have right. But the HAML view of the link tag and the active_admin parts of the config assets drove me for misunderstanding. And you forget an important part to mention. I must remove this line from the application.css: *= require_tree . And I used this to make it work on the view: <%= stylesheet_link_tag "subsheets/#{@css_to_use}" unless @css_to_use.blank? %> Pls edit your post with the application.css thing for later readers. Thank you. – Kael Aug 01 '12 at 10:48
  • 1
    yes, you need to remove ```require_tree``` otherwise all scripts will be included. i thought that this would be obvious. – phoet Aug 01 '12 at 13:56