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?

1 Answer 1

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/

Sign up to request clarification or add additional context in comments.

4 Comments

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?
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...
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.
yes, you need to remove require_tree otherwise all scripts will be included. i thought that this would be obvious.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.