Rails 3.1 assets pipeline in production - ruby-on-rails-3.1

I am using the assets pipeline (in Rails 3.1.3) and am kind of struggling to make it work in production.
Situation
In my /app/assets/stylesheets directory I have the following files:
application.css --> this is the default rails one
stylesheet.css --> This is my custom stylesheet
I spent a lot of time getting my stylesheet.css included in the /public/assets/directory in production (by running rake assets:precompile) and I finally made it by adding the following line into in my application.rb file:
config.assets.precompile += ['stylesheet.css']
I know have the right precompiled stylesheet.css file in production.
My Problem
The problem I have is when using stylesheet_link_tag with my stylesheet.css file. It turns out:
<%= stylesheet_link_tag "stylesheet" %> is resolved into <link href="/stylesheets/stylesheet.css" media="screen" rel="stylesheet" type="text/css"> in production I would expect the path to be resolved into /assets/stylesheet.css just like it does in development.
What is even more surprising is that application.css behaves perfectly even though <%= stylesheet_link_tag "application"%> resolves into <link href="/stylesheets/stylesheet.css" media="screen" rel="stylesheet" type="text/css">. What I don't understand is that the public/stylesheets/ directory does not exist in rails 3.1.
Any idea ?

Richard Hulse answers pointed me to the right direction. What happens is really subtle..
The answer to my question is Rails 3.1 assets has no fingerprint in production.
Basically, my project use mongoid instead of ActiveRecord. According to Mongoid documentation about configuration, the application.rb file can be modified to not include ActiveRecord which means removing:
require railties/all
And replacing it with:
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
# require "sprockets/railtie" # Uncomment this line for Rails 3.1+
I was so used to doing this manipulation with rails 3.0.x that I did not pay attention to the comment related to Rails 3.1
My problem was that I was not requiring sprockets !
Thank you all for your help !

The reason you can access stylesheet.css in development is because of how Sprockets works.
In development mode ALL requests to anything under /assets are sent to Sprockets to process. Sprockets will directly map requests to paths, one-to-one, so you can access any assets stored in app/assets/etc.
All requests go through Sprockets; it is serving the files to your browser.
In production things are different. A fingerprint is added to filenames, and the expectation is that you'll precompile your assets to static files. This is for performance reasons - Sprockets is not fast enough to serve lots of requests.
Only those CSS and JS files referenced by the default manifests are compiled into application.css and application.js. Other files that you reference are not precompiled unless they are added to the config.assets.precompile array in your config file.
You say that the files resolve to /stylesheets/stylesheet.css. The pipeline should generate a path like this in development: /assets/applicaton.css. In production there should be a fingerprint in the filename. What you have posted suggested that the pipeline is not enabled (these are the old, pre 3.1, locations for the files).
If this is an upgraded app, it is likely that you have missed some crucial config option. This is the main cause of dev->production issues. Check that the pipeline options are set exactly as they are in the last section of the pipeline guide. (My guess is that you are missing config.assets.enabled = true in application.rb)
And for clarity I would suggest changing the name of stylesheet.css to admin.css, while including this in the precompile array (as you already had done).
With the config options set correctly, and your admin manifest included in precompile, you should have application.css available for the front end and admin.css available for the back-end, both linkable via the helper methods.

You application.css should be a manifest file, meaning that your when you run your program it should include your stylesheet.css automatically.
make sure it has these two lines.
application.css:
/*
*= require_self
*= require_tree .
*/
If it does then something else isn't working properly, you shouldn't need the line:
config.assets.precompile += ['stylesheet.css']
If that isn't working either make sure you have all the settings enabled from this Asset Pipeline guide.

While I was looking around actionpack to see what might be causing this problem I found this little gem in the 3.1.1 changelog:
javascript_path and stylesheet_path now refer to /assets if asset pipelining is on.
Soooo, If you're using 3.1.0 upgrade to 3.1.1 and see if it fixes it. If you've already upgraded then were back to square one. But it does seem to describe your problem since stylesheet_link_tag uses stylesheet_path interally.

Related

Unable to load the assets in production in rails5

I am using Rails 5 , I have changed the application.html.erb to login.html.erb & make the corresponding changes in assets.rb.
My application works fine in development environment, but when
I run it in production, the css & js are not loading.
I have executed rails assets:precompile first, but it doesn't works, So is it really needed to have application layout in the rails5?
I found a solution for my question.
To make , it work I have to add 'rails_12_factor' gem in my gemfile.
Basically, rails5 added all its functionality except rails_serve_static_assets which is required in production.rb
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
If I don't want to add this , then I have to set the above settings as true to make it work.
Layout with different name is working.

Set headers in Padrino

I am attempting to remove caching and discovered this post:
How to prevent browser page caching in Rails
The problem it along with a number of other posts relating to ruby rails refer to an application_controller.rb file that my project does not have. I inherited this system so wasnt part of its initial development. Can someone explain why this would be the case? I am using padrino and in my config folder i have the following files:
Deploy
production.rb
staging.rb
Recipes
base.rb
nginx.rb
rbenv.rb
apps.rb
boot.rb
From what you've provided me I can say that I'm quite certain the project is using the Padrino ruby framework and not rails :)
See: http://www.padrinorb.com/

Grails Asset Pipeline unchaged

I am new at Grails and I have something to ask who is expert in Grails. I use asset pipeline as resources management in my project. Everything is good, But there is an issue that, whether my resources file (scss filess, coffee script files, ...) is changed or not, the resources are compiled every time views are rendered (in dev and test environment). This makes project run slow. Is there any solution to cache resource in asset pipeline, therefore if there is not any changing, the resources are not compiled. Thanks!
If you are using require to build a require tree and then refer the tree in your views then you can directly exclude raw resources getting pre-compiled every time by the plugin. For example:
If you have a require tree under grails-app/assets/javascripts/application.js as
//= require jquery
//= require app/models.js
//= require_tree views
//= require_self
or .coffee
#= require app/models.js
#= require test
#= require_self
#= require_tree .
And you don't want models.js getting pre-compiled everytime the view using the require tree is rendered then add the configuration as below:
grails.assets.excludes = ["app/models.js"] //app/*js for all resources under app
Above config informs the plugin to avoid the precompilation of resources and will only be compiled when the asset is referred in the view and asset has any changes.
You can find more on the Usage documentation, mainly
Optionally, assets can be excluded from processing if included by your
require tree. This can dramatically reduce compile time for your
assets.
Above config can be environment specific and can be used only for dev and test. For Production environment and/or war the pre-compilation would not matter.
environments {
development {
grails.assets.excludes = ["app/models.js"]
}
}

Requests for unneeded CSS after asset compilation on Heroku

In both development and production my app seems to work fine. However, I noticed it was making some requests for CSS files that were returning with 404s.
Rails 3.1.1
Heroku "Cedar" stack
the compiled production CSS "application-b3ce81dc0178ccf6b6ac77b8bc7774ef.css" begins with..
#import url(jquery.ui.base.css);#import url(jquery.ui.theme.css);#import url("jquery.ui.core.css");#import url("jquery.ui.autocomplete.css");
namely - the files that are resulting in 404s so it looks like I'm directly requesting files that have already been compiled into the application.css
application.css.scss:
/*
*= require vendor
*= require_self
*= require_tree .
*/
#import "bootstrap";
.. some more regular CSS ..
removing the require vendor line gets rid of the duplicate requests, but also results in my site missing required styles..
Any ideas?
You cannot use the CSS import with the asset pipeline as the files are not available normally, only via their fingerprinted versions.
You need to have the correct files in your application.css (via the manifest in application.css.scss) or use the Sass helpers to put the correct asset URLs in your code.
http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets

Rails 3.1: Adding .scss stylesheets

Just trying to get to grips with the new Rails 3.1 way of managing assets. I've copied across a .scss stylesheet (style.css.scss) to app/assets/stylesheets/, and added
= stylesheet_link_tag :style
To the app/views/layouts/application.html.haml file. I had expected that was all I needed to do - but no joy; scss doesn't seem to be generating the css sheet. When I view the 'style.css' source in page source (yes, it's called in the html source) it says the following:
Routing Error No route matches [GET]
"/assets/style.css"
So...any ideas how to get 3.1 to generate the actual stylesheet from the scss?
Many thanks...
Try adding a style.css file to your assets/stylesheets/ directory that looks like this:
/*
*= require style.css.scss
*/
That should automatically convert your style.css.scss to plain CSS and include it in what /assets/style.css returns.
Don't know if the problem you described was due to Rails 3.1 not being released yet, but the "standard" Rails 3.1 way would be to put style.css.scss in your app/assets/stylesheets folder and then linking to application.css which is a socalled manifest file that includes all other files from the folder.
You would then be using
= stylesheet_link_tag 'application'
instead.
Best regards,
Lasse

Resources