Kael
Kael

Reputation: 563

Rails Asset Pipeline with conditional CSS files

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?

Upvotes: 3

Views: 2687

Answers (1)

phoet
phoet

Reputation: 18835

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/

Upvotes: 3

Related Questions