I have a JS file defined in the app - app/assets/javascripts/client/some-client.js.coffee
In my dev env I can access this file via URL - /assets/client/some-client.js
But I cannot do the same in production? ...meaning the URL does not work, what could be wrong?
In prod, you will have access only to precompiled files, if you use the default configuration.
You can define which assets to precompile in config/environments/production.rb:
config.assets.precompile += %w( some-client.js blabla.js some-client.css ) #etc...
Run rake assets:precompile to... precompile your assets.
Learn more with this guide: http://guides.rubyonrails.org/asset_pipeline.html
If you didn't find it yet, be sure that you are accessing it through <%= javascript_include_tag "some-client" %>. As the asset pipeline handles finding where it has precompiled the asset. Along with the config.assets.precomile += %( some-client.js ). Also, if the helper function can't find that in production you might want to try moving client/ to assets/javascript/client I think it looks for assets for example javascripts files in app/assets/javascripts/ lib/assets/javascripts/ and vendor/assets/javascripts/ if I understand the asset pipeline. And it looks like you don't have it in any of those files, so it may be skipping it since it doesn't see it.
EDIT:
Looks like you have it in javascripts/ sorry. The asset pipeline should traverse subdirectories.
Related
I'm trying to deploy my first Rails application, but I'm getting 404 Not found for all assets (.css, .js files and images). I tried many ways of getting things work, but nothing worked. All of the asset paths look like this:
/stylesheets/application.css
My production.rb file: http://pastebin.com/SeVNEZD9
My application.html.erb, where I include these assets look like this: http://pastebin.com/gHkpfA8Z
Thanks forward for any help.
In config/environments/production.rb file add
config.assets.digest = true
Then stylesheet_link_tag will generate fingerprints url.
You need to precompile your assets for this by running this command on terminal.
RAILS_ENV=production bundle exec rake assets:precompile
After deployment you should do
RAILS_ENV=production bundle exec rake assets:precompile
Since this is your first Rails app, let me explain where your problem might be:
When you create your assets, they reside in the asset pipeline. This
is a folder located at /app/assets where your asset files will be
stored
If you want to use assets in production, there are two ways to do it -
the first is to use the assets dynamically, or to use them as static
(which is where you precompile). When you use the likes of Heroku etc,
you need to use static assets, which is why they precompile them for
you when you deploy
The problem with static assets is that Rails creates fingerprints
for them. These fingerprints basically give them a unique identifier
which you have to dynamically reference using <%= asset_path %> etc
Your problem is likely caused by you not accessing the precompiled assets correctly. The way to fix this is to use asset tag helpers like this:
#app/views/layouts/application.html.erb
<%= stylesheet_link_tag "application" %>
<%= javascriot_include_tag "application" %>
It looks like it's working now. I needed to restart the server to apply the changes. Can anybody tell me why is it that I have to restart the server to apply such changes? I want to avoid it in the future.
I'm posting my production.rb file for those who face similar problems: http://pastebin.com/7Uskvcuc
I added a file named mobile.css into my assets/stylesheets directory. This file is not required in application.css as I only explicitly add it by pages I want optimized for mobile. When I run rake assets:precompile it doesn't push it into the asset pipeline. I'm going to guess if I add it to application.css it'll precompile, but then my mobile stylesheet will override the default stylesheet which I do not want.
Works great in development mode with the following in my layout:
- if mobile_device?
= stylesheet_link_tag "mobile"
In production this is a no go as mobile.css is not getting added to the pipeline.
What's the best way to handle this?
You can always add it manually to the precompile array in your application.rb.
config.assets.precompile += %w( mobile.css )
Rule of thumb as far as the asset pipeline is concerned: if it's not required in a manifest OR it's not in the precompile array, it's not going to be precompiled.
How to make Apache+Passenger and Rails Asset Pipeline work together nice?
When I deploy my locally working project, I get a 500 error
In ActionView::Template::Error occurred in pages#start: jquery-ui-1.8.21.custom.min isn't precompiled
If I grep my_project/current/assets_manifest.yml for jquery-ui-1.8, it gives me
jquery-ui-1.8.21.custom.min.js: jquery-ui-1.8.21.custom.minc50ea0bef9c2fae04ab3b50ead60fc1f.js
and this file also exists in my_project/shared/assets (along with jquery-ui-1.8.21.custom.min-c50ea0bef9c2fae04ab3b50ead60fc1f.js.gz, jquery-ui-1.8.21.custom.min.js, jquery-ui-1.8.21.custom.min.js.gz).
When I open
http://mytestserver/assets/jquery-ui-1.8.21.custom.min-c50ea0bef9c2fae04ab3b50ead60fc1f.js
in browser it gives me the correct js file.
The 500 error is raised from
app/views/layouts/application.haml:25
line 24-26 of that file are:
= javascript_include_tag "application"
= javascript_include_tag "jquery-ui-1.8.21.custom.min"
= csrf_meta_tag
So what could have gone wrong? Why is it not working?
If the file isn't being loaded by application.js then you need to add a line to application.rb so the app knows about it:
config.assets.precompile += ['jquery-ui-1.8.21.custom.min.js']
shared/assets is not a place that the asset pipeline typically looks for files.
Typically the asset pipeline looks in app/assets, it may look in lib/assets and I'm pretty sure it looks in vendor/assets also. But it's possible that the vendor/assets is not included by default, which I talk about below.
Per Rails convention I suggest you put that dependency in vendor/assets/javascripts
If you want to add search directories to the asset pipeline it's just a simple modification to config.application.rb
in the YourProject::Application declaration, add:
config.assets.paths << Rails.root.join("vendor/assets/javascripts")
Or whatever other path you want.
I think I solved it.. I think this were the steps:
renaming the rake-namespace of capistrano deployment (possible duplication of namespace and var damage) which was also doing the precompilation
add each js file that I include via javascript_include_tag to config.assets.precompile for production environment like #Simon suggested
in config.assets.precompile they seem to be mentioned without .js-extension, whereas in the javascript_include_tag directives they should be included with .js-extension
I have a style sheet file I put under "vendor" folder
vendor > assets > stylesheets > style.css
on my development machine if I try to access it with "/assets/style.css" I can.
In production it's not accessible. The styles on the page are broken:
ActionController::RoutingError (No route matches [GET] "/assets/style.css")
Ideas how I can make that work?
Reason why I put it in Vendor is that I only wanted those style to be include in specific pages and not be compiled and included across the site, if there is a better way to include those assets only for specific pages please advise.
BTW, I'm deploying my app on Heroku.
To access this in production you need to add that file to your precompile array in application.rb:
config.assets.precompile += ['styles.css']
You should reference the file in your view with the Rails' helpers:
stylesheet_link_tag('styles')
as this ensures the correct fingerprint is generated in the source.
I don't think the fact this stylesheet is in vendor path is a problem since sprockets should search in vendor too (from what I'm reading in the Asset Pipeline guide).
How do you try to access this CSS file? Have you tried to run
$rake assets:precompile
add/commit and re-push to Heroku remote?
Setup
error.sass is under app/assets/stylesheets
I ran bundle exec rake assets:precompile
error.css is in the manifest error.css: error-8f9fb7a53be409476d28603c33a7cd1d.css
Problem
error.css isn't precompiled
Other odd things that may indicate problems with my setup
In [environment].rb config.assets.compile = false. This is desired. When I turn it to true it works, but I don't want live compile
Everytime I load a page public/stylesheet gets generated with all the scss/sass files (but not css)
This is an upgrade from rails 3.0, but I think the upgrade was succssful
Help?!
By default css and js files (except application.js and application.css) are not precompiled. It looks like you can add config.assets.precompile += %w( errors.css ) to fix your issue. Also, there's more info about precompiling here: http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets
Have you tried restarting the server after you precompiled the assets?
The server will stick to the manifest.yml it had when you started the server.
(I know it's a late answer but I just had the problem :) )