How to Turn Off Automatic Stylesheet/JavaScript Generation on Rails 3.1

How do I turn off automatic stylesheet/javascript generation on Rails 3.1?

Add these lines to application.rb:

config.generators.stylesheets = false
config.generators.javascripts = false

How to disable the generator of assets on rails 3.2

You can pass --skip-assets to your command to prevent these files from being created:

rails g controller foo --skip-assets

If you want something more permanent, you can turn it off altogether. Add this to config/application.rb (from How do I turn off automatic stylesheet/javascript generation on Rails 3.1?)

config.generators.stylesheets = false
config.generators.javascripts = false

How can I completely disable CoffeeScript in a Rails 3.1 app?

  1. Comment out gem "coffee-script" in your Gemfile
  2. Use .js instead of .js.coffee for your javascript files

rails 3.1 - auto compile SASS before browser refresh

fixed this using guard-livereload - now my code changes take effect before I've even switched from text-editor to browser, which cuts down on RSI-inducing mouse movements considerably

My stylesheets in Rails seem to be overlapping

This is due to Sprockets & bad CSS structuring.



#app/assets/stylesheets/application.css
*= require_all

It's not CSS which will be the problem, but the way you're calling it - both in your asset pipeline and your application.

The above will be concatenating all your css into the main application.css file. Whilst this might seem harmless, if you have conflicting CSS definitions, they'll override themselves.

Sample Image

The way around this is to firstly ensure you're not conflicting your CSS and you are calling only the appropriate CSS for your application.


Sprockets

To better understand a fix, you need to know about sprockets.

Sprockets compiles your assets to make them as efficient as possible.

Your assets reside in the asset pipeline, which has been created to be such that it will load the appropriate CSS for the different parts of your app -- both efficient and versatile.

The "conventional" way for Rails to do this is to encourage you to use controller centric assets and application level assets:

#app/assets/stylesheets/controller.css
#app/assets/stylesheets/application.css

This is okay if your CSS works in situ, but if it doesn't, you've got a problem (like what you're experiencing).

As such, the way to fix is to firstly ensure that you're splitting your CSS as you need, and you're coding the CSS properly (more in a second).



Controller-Centric Assets

1. Remove all references in "application.css"

Sprockets gives you the ability to "include" other files in your main application.css file:

#app/assets/stylesheets/application.css
*= require_all <- remove this line

This will prevent your "application.css" file having all the others compiled into it. The others will be compiled on their own, allowing you to call them separately:

2. Layout

#app/layouts/application.html.erb
<%= stylesheet_link_tag "application", controller_name %>

This will allow you to call the controller-centric assets you have with base-level application CSS.

The drawbacks to this are that you'll have to repeat a lot of your CSS, and will end up having completely different CSS for different parts of your app -- not the best.



CSS Structure

The alternative is to make your CSS structured properly. This is much harder to achieve, but will give you much clearer results...

I have a main div with different width for my Sessions controller (login page) and my Records controller (search function.)

#app/assets/stylesheets/application.sass
.main
/* main styling */
&.search
/* search styling here */
&.results
/* results styling here */

This will allow you to call:

#app/views/layouts/application.haml
.main.search search stuff here
.main.results results stuff here

Store a value with Rails

You could add the salaryPaid variable to the employee model, and update it each time you pay the salary (or whatever is equivalent for your app). This would still use a database but it would just make a new entry for each employee and would be easy to manage with migrations.

Using Rails 3.1, where do you put your page specific JavaScript code?

I appreciate all the answers... and I don't think they are really getting at the problem. Some of them are about styling and don't seem to relate... and others just mention javascript_include_tag... which I know exists (obviously...) but it would appear that the Rails 3.1 way going forward is to wrap up all of your Javascript into 1 file rather than loading individual Javascript at the bottom of each page.

The best solution I can come up with is to wrap certain features in div tags with ids or classes. In the javascript code. Then you just check if the id or class is on the page, and if it is, you run the javascript code that is associated with it. This way if the dynamic element is not on the page, the javascript code doesn't run - even though it's been included in the massive application.js file packaged by Sprockets.

My above solution has the benefit that if a search box is included on 8 of the 100 pages, it will run on only those 8 pages. You also won't have to include the same code on 8 of the pages on the site. In fact, you'll never have to include manual script tags on your site anywhere ever again - except to maybe preload data.

I think this is the actual answer to my question.

Asset Pipeline: excluding an admin.css file

Similar question was asked earlier and you should check that one.

Sprockets uses manifest files to determine which assets to include and serve. These manifest files contain directives — instructions that tell Sprockets which files to require in order to build a single CSS or JavaScript file. With these directives, Sprockets loads the files specified, processes them if necessary, concatenates them into one single file and then compresses them (if Rails.application.config.assets.compress is true). By serving one file rather than many, the load time of pages can be greatly reduced because the browser makes fewer requests.

You can have as many manifest files as you need. For example the admin.css and admin.js manifest could contain the JS and CSS files that are used for the admin section of the application.

In particular, you can specify individual files and they are compiled in the order specified.

Example and more details can be found in this guide.

Thus, your new application.css would become:

/*
*= require styles
*= require layout
*/

/* Other styles */

Capistrano generating empty application.css stylesheet when deploying

I've found the answer (at a high level at least).

It appears it has something to do with Ruby 2.0.

When I set RVM on the server to use Ruby 1.9.3, and did another deploy it worked**.

I'm new to RVM so what I did might be a slight overkill:

rvm install 1.9.3
rvm use 1.9.3
rvm --default 1.9.3

** I also re-introduced the following line of code in config/application.rb. I'd previously commented it out as part of my attempt to get this to work:

Bundler.require(*Rails.groups(:assets => %w(development test)))


Related Topics



Leave a reply



Submit