rails beginner here
I am trying to set an app to production but having big issues with the asset pipeline. Under development every asset is generated under /assets/blabla.extension
However, I am running nginx and when running rake assets:precompile it creates assets under /public/assets as it should. But when I visit my app it generates urls like /application.css and not /assets/application-digestq12343.css as it should.
I think there is an easy fix to this problem.. but I cannot find it. Please help me!
Update:
From the documentation I read that Sprockets "default" is /assets but it certainly isn't in my application. I use the latest version of rails. Can the documentation be outdated?
http://edgeguides.rubyonrails.org/asset_pipeline.html
Even if I add config.assets.prefix = "/assets" to my production it would still mean that I would not load the assets since the digest would be missing.
I believe this is the same problem I was experiencing. Instead of using url()in your css (or scss), use asset-data-url() and add config.assets.enabled = true to your application config file. Then, rake assets:precompile before you push.
Related
Assets on Heroku are not updating with new code/assets/css.
I tried heroku rake assets:clean. I right clicked an image. I can see the code still has the old compiled asset hash in the public dir.
I even tried bumping the asset version.
Rails.application.config.assets.version = '2.0'
I am on Rails 5. This below line is in the Rails default production config. I didn't know apache handled serving static assets. Is that something new?
It also seems like Heroku is getting assets from somewhere else. Another server? If I delete the entire public dir right on the server, it still serves the assets on the website.
Comment
Disable serving static files from the /public folder by default since
Apache or NGINX already handles this.
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
Are you seeing this output when you push to Heroku?
-----> Preparing Rails asset pipeline
Detected manifest.yml, assuming assets were compiled locally
That means it is still seeing a manifest file. Delete the public/assets directory to remove it with rake assets:clobber. Then you need to add these changes and then push to Heroku.
I have deployed this app (rails 3.2.11) a million times, I haven't messed with any settings, but now I'm greeted with this:
Why did this happen out of the blue? My conents of application.rb include config.assets.enabled = true - never had any issues.
In fact running it locally on port 3000 seems to not have any issues whatsoever.
After deploying to heroku this morning, it seems that it loads nothing that's inside /assets/
Interestingly, after copying the files over to try and just make a new app, git commit results in all the stuff you'd expect as well as a LONG list of these which I think might be related:
Edit: Interestingly enough SOME of the assets have loaded, like the logo and the background, but the rest as you can see return 404.
put line in config/environments/production.rb
config.assets.compile = true
it worked as it will compile the assets on runtime, just like in development environment, but its makes application slow, the best way is to either compile the assets locally in production environment with rake task(RAILS_ENV=production bundle exec rake assets:precompile) and commit your generated assets in public/assets and then do deployment. or, heroku run rake assets:precompile
I had this problem today with rails 4 on heroku. The article provided by #Jeff is a little bit old but, the gem repository has a good readme.
Summarizing, you will need to add two gems to your Gemfile:
gem 'rails_serve_static_assets' (it will solve the static assets problem) and
gem 'rails_stdout_logging' (which the previous one depends on).
Heroku released a gem to handle the assets without needing to turn off compilation or to manually compile.
https://devcenter.heroku.com/articles/ruby-support#static-assets
Just add this to your Gemfile and redeploy.
gem 'rails_serve_static_assets', group: [:production]
For Rails 4, use:
config.serve_static_assets = true
The default was false. We needed this after removing the rails_12factor gem.
Rails recommended that this setting config.serve_static_assets by default should be disabled i.e. set to false. Here is the default configuration in config/environments/production.rb generated in rails app
Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
So if you are setting it to true in your local app then that's still fine. But if you are deploying your app on Apache or ngix or anything other than heroku then its not advisable to make config.serve_static_assets=true in your production.rb config file. Here is the documentation from Rails guides.
config.serve_static_files configures Rails itself to serve static
files. Defaults to true, but in the production environment is turned
off as the server software (e.g. NGINX or Apache) used to run the
application should serve static assets instead. Unlike the default
setting set this to true when running (absolutely not recommended!) or
testing your app in production mode using WEBrick. Otherwise you won't
be able use page caching and requests for files that exist regularly
under the public directory will anyway hit your Rails app.
URL - http://guides.rubyonrails.org/configuring.html
To make the assets to load with the corresponding fingerprint of each file verify the configuration config/environments/production.rb has the instruction:
ruby
# Load assets with fingerprint behavior
config.assets.digest = true
Using Rails 3.2.2 and ruby 1.9.2 and I am not able to serve assets in production. We are running on apache. I've read some documentation and set :
config.serve_static_assets = true
config.assets.compile = true
in production.rb, and it doesn't seem to work. What else can I try? Or how can I get some debug info on where it IS looking.
Also, I went back to development, and went into assets/images and deleted .png files in order to intentionally break things in the development environment, but it didn't work, the images still show up. There must be some pre-compiling of assets, but where do they get stored, and how would I clear that? thanks
Usually, if you have Apache in front of your rails server, you would not want your application to compile assets in production. You probably want to pre-compile assets and have Apache serve them.
Compiled assets are stored in public/assets by default.
How all this comes together depends on the particulars of your configuration, which you have not shared. I would recommend reading the rails guides on the asset pipeline:
http://guides.rubyonrails.org/asset_pipeline.html
and in particular:
http://guides.rubyonrails.org/asset_pipeline.html#in-production
It heven has some examples of how to configure Apache.
In config/application.rb:
config.assets.enabled = true
Lately I have to precompile my assets to see my css changes in my development environment. I don't know why this, the environment setting config.assets.debug is still set to true. I find this rather annoying, I believe I didn't have to run precompile after every change before. How do I make rails compile on page load again?
I'm running Rails 3.1.3. I don't know what other info to provide, please let me know what you need to know to solve this.
Try completely removing your compiled assets and it should reset to the default development behavior. Otherwise, Rails will keep trying to load your precompiled public/assets/ files before loading the stuff under app/assets/:
rake assets:clean
I am new to rails and using version 3.1.0 and trying to deploy to heroku. I have a very simple application that i have managed to get up and running on heroku after working my way through several issues. I used the command
bundle exec rake assets:precompile
to get heroku to load the assets for the application (i read that heroku requires this for version 3.1.0). The problem is that when i try to run the application, the server gives the error
ActionController::RoutingError (No route matches [GET] "/assets/all.js"):
i have all of the required javascript files in public/javscripts but i cannot find any version of all.js in the assets directory. I have included
config.assets.precompile << '*.js'
in config/application.rb but to no avail.
I was wondering if someone could tell me how I get the precompile to create all.js in the assets directory or if I am completely off track here. i was under the impression that the precompile function compiled all of the javascript files into a single optimized file.
Also, if anyone has any good links for a beginner to understand how the precompile functionality works in rails I would be very appreciative.
Thanks in advance.
The best guide is the official rails guide. There is also the Railscast about it.
In a nutshell your Javascript files go in app/assets/javascript from where the precompile task will process them and put then in the public/assets directory. You should not have to change the precompile config option if the follow the defaults. Don't forget to the use the correct helpers to reference your files.
Read the resources above, and if you are converting an existing app watch the Railscast and follow the last section of the guide - these should point you in the right direction.