Felix
Felix

Reputation: 5619

application.css for different layouts how to set correctly?

In my Rails application i've got several layouts and for each I want to load special CSS how can I do this?

at the moment application.css looks like this:

//= require bootstrap.min
//= require filechooser
//= require bootstrap-switch.min
//= require jquery.datetimepicker
//= require fancybox2_1_5/jquery.fancybox
//= require style
*= require_self
*/

Upvotes: 0

Views: 280

Answers (2)

Matthew Lehner
Matthew Lehner

Reputation: 4027

I like to use a specific JS/CSS for file for each layout (when applicable).

For example, app/layouts/application.html.erb would have your application.css, like this:

<%= stylesheet_link_tag 'application' %>

Whereas app/layouts/admin.html.erb will have admin.css:

<%= stylesheet_link_tag 'admin' %>

Using Sprockets 3 or later, you can even use index files in the directories to keep things organized. app/assets/stylesheets/admin/index.css will be available as admin.css. See Index Files are proxies for folders for more info.

Upvotes: 2

dp7
dp7

Reputation: 6749

If you are using different layouts for different controllers, then you can use your special css or js files directly in your layouts by adding a method to your ApplicationHelper as follow:

def controller_stylesheet_link_tag
  if params[:controller] == "foo"
    stylesheet_link_tag 'foo'
  else
    stylesheet_link_tag 'bar'
  end
end

Then, add this helper method to your layouts like application.html.erb -

 <%= controller_stylesheet_link_tag %>

Upvotes: 2

Related Questions