Rocky
Rocky

Reputation: 5716

How to include different css file dynamically with asset pipeline in Rails 3.1

I am upgrading to rails 3.1. in my old application.html.erb, I have a line of this:

<%= stylesheet_link_tag "themes/#{session[:theme].nil? ? 'base' : 
     session[:theme]}/ui.all" %>

As you know, I want to change the style base on the session[:theme]. Thanks to This link, I made a little progress. I modified my application.css.erb to this:

/*
* *= require_self
* */
<%
    require_asset("themes/#{session[:theme].nil? ? 'base' : 
                    session[:theme]}/ui.all" )
k%>
/* rest of file omitted */

But it complains this:

undefined local variable or method `session' for #<#<Class:0x95152e4>:0x9c6c8bc>
 (in /home/rocky/work/apps/fanfan/app/assets/stylesheets/application.css.erb)

Upvotes: 1

Views: 859

Answers (1)

Richard Hulse
Richard Hulse

Reputation: 10493

The application manifest is compiled at deploy time, or compiled and cached if you are serving assets live, so you have two problems.

The first is that there is no session value available, and the second is that the even if you could change this it won't work; it would be cached the first time the manifest is compiled.

You can go back to your initial solution, and make one addition to your config for it to work.

config.assets.precompile += ['themes/theme1_name/ui.all', 'themes/theme2_name/ui.all', etc]

I am assuming here that themes is in the default stylesheets location.

Upvotes: 2

Related Questions