I am able to precompile assets in public/assets directory of rails application.
But when I start application in production, rails constructing wrong path and not pointing to the precompiled assets.
System environment details are as below.
rails -v
Rails 5.0.4
ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [i386-mingw32]
"sprockets-rails", '2.3.3'
and production.rb
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
config.public_file_server.enabled = true
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = false
and asset.rb
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
Rails.application.config.assets.precompile += %w( *.js *.css *.js.erb *.css.erb)
And my index.html.erb looks this way
<%=javascript_include_tag "bootstrap.min"%>
<%=stylesheet_link_tag "bootstrap.min"%>
and the rails trying to fetch assets using below paths,
and the compiled assets in folders looks as bellow.
Kindly help me to find out where I am going wrong.
Thanks in advance and any help appreciated.
Make sure the digest setting is enabled:
config.assets.digest = true
Related
I have searched around and can't rectify the situation based on the responses to other similar questions. I seem to have broken the asset pipeline somehow but can't seem to figure out how.
None of my assets are being loaded at all; rails seems to just be ignoring my manifest files. When I inspect my page in firebug, only the 'non-compiled' text inside my manifest files (both js and css) is being displayed - almost as if the asset pipeline wasn't even enabled.
I deleted the contents of public/assets since I was adding a new file to the manifest which seemed to start this behavior.
Current configuration:
environments/development.rb
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
application.rb
# Enable the asset pipeline
config.assets.enabled = true
config.assets.manifest = config.root
# Add extra assets for precompiling
config.assets.precompile += ['admin.js', 'admin.css']
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
I had the same issue. You can still use Ruby 2.1.2, just upgrade rails to latest 3.2.x version - it's 3.2.22
Gemfile:
gem 'rails', '3.2.22'
And run:
$ bundle update rails
The issue was caused by using an incompatible version of ruby. I was using version 2.1.2 which lead to unusual behavior from the sprockets gem (which powers the asset pipeline). This was fixed by downgrading to ruby 1.9.3. I haven't done any experimentation for fear of breaking it again but maybe this has been addressed in later versions of sprockets. I am using sprockets 2.1.3.
See: Rails 3.2.8 Application.js and Application.css are not working as expcted
Always remember two things when you want to handle Rails asset pipleline:-
if you want all you newly created js/css to autoinclude in application.js/css,pls add them in...
ELSE
IF you dont wont to add in manifest file(application.js/css) then use precompile directive in yuur environment file.
config.assets.precompile=%w(custom.css,custom2.js...etc)
so make sure you have either of these...
===========for example=-=============
suppose you have new css/js file:-
custom.css inside
app/assets/stylesheets/
so you can include in
application.css
// = require 'custom'
OR
use precompile directive:-
config.assets.precompile += %w( custom.css )
and then reference it like you always do
stylesheet_link_tag "custom"
same applies for js also
I just spent a few hours troubleshooting this issue (in 2017!) and it turned out I just needed to remove the gem active_reload. Updating rails and ruby was having no effect for me. The thread where I found that gold is here:
https://github.com/rails/rails/issues/2715
I was reading the documentation on Rails 4 and the Asset Pipeline when I got tripped up by this line:
If you are upgrading from Rails 3, please take into account that assets under lib/assets or vendor/assets are available for inclusion via the application manifests but no longer part of the precompile array
http://edgeguides.rubyonrails.org/asset_pipeline.html#asset-organization
What is the difference between being available for inclusion via the application manifest and being part of the precompile array? Does this mean that if I were to have a javascript file lib/vendor/cool_plugin.js I would get a 404 when I went to production even if I had //require cool_plugin in application.js?
From http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html:
In Rails 4.0, precompiling assets no longer automatically copies
non-JS/CSS assets from vendor/assets and lib/assets. Rails application
and engine developers should put these assets in app/assets or
configure config.assets.precompile.
so I believe you need to explicitly reference your vendor assets for them to get picked up during precompilation on production:
# production.rb
config.assets.precompile += %w( cool_plugin.js )
I just updated a Rails 3.2.x app to 4.0.2.
When I deployed to production (ubuntu, MRI 2.0) I got the good old error about the lack of a javascript runtime.
I quickly fixed it by installing node, but it makes me wonder.
I prefer to precompile the assets locally, check them into git, and then push them to the production server along with the rest of the application.
With Rails 3.2 this system has always allowed me to not care about a js runtime in production, as the application doesn't need to compile coffeescript or run uglyfier.
So, the question is: what has changed with Rails 4? Is there a config option to control this behaviour?
I checked my (rails 4) config file, and I think that the production evironment is already configured to NOT fallback to live compilation.
assets-related config options:
config/application.rb
config.assets.precompile += ['html5shim.js']
config.assets.initialize_on_precompile = false
config/environments/production.rb
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.js_compressor = :uglifier
# config.assets.css_compressor = :sass
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
In case someone hit this question as I did. I had the same problem and this link pointed me to the right direction: https://mattbrictson.com/upgrading-to-rails-4-with-capistrano. Specifically the following statement: In Rails 4, the standard Gemfile no longer has an :assets group, which means asset pipeline gems are always loaded in production, on all servers. I precompile my assets locally and upload them to production, so there is no reason to have JS runtime on production server.
In my case I added group :asset to my Gemfile putting there asset-related gems. In my case it was:
group :asset do
gem 'uglifier'
gem 'execjs'
end
My capistrano tasks on production install bundle without :asset group, so after this change JS runtime is no longer required on production.
I'm using twitter-bootstrap-rails-2.2.7
Rails-3.2.11
sass-rails- 3.2.3
Running application returns back:
ActionView::Template::Error ("fontawesome.less" wasn't found....
Well, I downgraded twitter-bootstrap-rails to version 2.2.6 :)
You need to add it in config.assets.precompile (config/environments/production.rb)
from https://stackoverflow.com/a/7278355/600953
By default Rails assumes that you have your files precompiled in the production environment, if you want use live compiling (compile your assets during runtime) in production you must set the config.assets.compile to true.
# config/environments/production.rb
...
config.assets.compile = true
You can use this option to fallback to Sprockets when you are using precompiled assets but there are any missing precompiled files.
If config.assets.compile option is set to false and there are missing precompiled files you will get an "AssetNoPrecompiledError" indicating the name of the missing file.
Rails just compress javascript in development mode but i dont need to.
Here is the config/environments/development.rb
# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true
Rails version is 3.2.8 but also tried 3.2.9-rc3, 3.1.8 doesnt work with my application because it was created in 3.2.8
I just found that rails serves precompiled assets instead of assets from app/ catalog when they are exist. Its need to remove them to make Rails work like i need.
rake assets:clean
I belive Rails must not behave that way in development mode.