Rails Asset Pipeline with Themes

I have an application that allows users to select a theme for their page. There are several topics for them.

The HTML structure for each page is the same with the same class names in the div. CSS is changing to fill the theme. When a user selects a theme, I save the topic id in the database and request it when you access the page by downloading the relevant file to the CSS application_layout.html.erb:

<%= yield(:theme_style) %>

In users_page.html.erbI will take the corresponding file with

<%= provide(:theme_style, "theme_styles/#{@user.style.style_filename_file_name}") %>

Since the page structure does not change, it is important that the CSS files are not compiled into one large CSS file, otherwise the latter will be the only one available, since it will overwrite all previous styles. How can I get Rails to handle theme files?

As production.rbI have config.assets.precompile += ['theme_styles/basic.css', 'theme_styles/two-column.css'], but it's not like a trick, as he tells me that he is not precompiled.

I can’t find enough information anywhere to put me in the right direction, and I have looked through blogs, SO questions and Rails documentation.

+5
source share
1 answer

I know this is old, but I decided that I would answer with what I did if it helps someone else. I had a similar need for my application, and after I did not find many results and launched IE with the maximum number of selection errors when using them in one css file controlled by user selectors, I ended the way so that each theme was different CSS by creating a folder structure that looks like this:

:

  • /app/assets/stylesheets/
  • /// //
  • /// /-1/
  • /// /-2/
  • .

app/assets/styleshseets application.css.scss( css, sass) , ,

 *= require_self

css , ( main/stylesheets). , , , , - , css, - , IE .

css

/// //

css, . , CSS , . ...

 *= require_self
 *= require_tree ./all

, :

/// /-1/

:

   *= require_self
   *= require_tree ./theme-1

, ( 2 1 ).

,

production.rb

  config.assets.precompile += %w( application-all.css application-theme-1.css application-theme-2.css, etc.)

, , , . ( , ):

class MyController < ApplicationController
  layout :serve_layout
...
  def serve_layout
    #do what you need to call your layout / theme...    
  end

, - .

+5

All Articles