0

So I'm kinda lost here. All my CSS/SCSS files are loaded everywhere on my app. But I have two different design (front and back) that I want to separate. How can I achieve that ?
Plus it's kinda useless that all js/css are loaded, even where they are not used. How can I control that ?

S. A.
  • 3,714
  • 2
  • 20
  • 31
Simon
  • 619
  • 2
  • 9
  • 23
  • possible duplicate of [Using Rails 3.1, where do you put your "page specific" javascript code?](http://stackoverflow.com/questions/6167805/using-rails-3-1-where-do-you-put-your-page-specific-javascript-code) – juanitogan Jul 23 '14 at 18:21

2 Answers2

1

In your application.js and application.css there is a directive by default: require_tree. It will load all your js and css files to be precompiled later. This is done to make the clients to download the assets packet only once (as it will be cached by the browser) and make the app faster.

If you want to load specific javascript or stylesheet files for each controller, remove the require_tree directive and include them in their respective controller:

<%= javascript_include_tag params[:controller] %> or <%= stylesheet_link_tag params[:controller] %>

Check this out: http://guides.rubyonrails.org/asset_pipeline.html#controller-specific-assets

S. A.
  • 3,714
  • 2
  • 20
  • 31
1

What you're wanting to do is control your layouts.

As your question is currently it's too broad for someone to give you a decent specific answer, it's like saying 'tell me about astrophysics, I don't understand how to launch a rocket right now'.

I would suggest to start with the rails guides relating to layouts and then come back with a more specific question once you have a better understanding.

http://guides.rubyonrails.org/layouts_and_rendering.html

There is also a great 11 minute video on RailsCasts which will help you understand and control the assets pipeline: http://railscasts.com/episodes/279-understanding-the-asset-pipeline

Where you are heading is say your app was about managing projects.

  1. Make a copy of the application.css file called say project-manifest.css and use the same structure as that application.css for loading just the stylesheets you want.

  2. Make a copy of views/layouts/application.html.erb to say projects-layout.html.erb

  3. In the new projecs-layout file, update the reference to the css to point to project-manifest.css

  4. Point your controller code to use your new layout

say you have:

# app/controllers/ProjectsController.rb

def show
  # code here
  # rails does a default render layout: 'application', its overwritten by adding an explict render
  render layout: 'project-layout'
end
Evolve
  • 8,939
  • 12
  • 51
  • 63
  • Well, to be more specific: I have a css file that overload some classes that I want to be used only on the front part of my app. Where should I put the file so it's not used on the whole app, but only on the front part ? – Simon Jul 15 '14 at 13:35
  • The instructions I've given you will help you do just that, watch that asset pipeline video and read the layouts guide quickly then my instructions should make sense. – Evolve Jul 15 '14 at 13:38
  • Every page you create in rails CURRENTLY includes the file /app/layouts/application.css you need to make a new layout file that points to it's own CSS and then point to that for the pages you want to. – Evolve Jul 15 '14 at 13:41
  • As you are starting out id also suggest you download the x-ray rails gem which helps you see visually what comes from where in your rails app. https://github.com/brentd/xray-rails – Evolve Jul 15 '14 at 13:42