Rails 3 serve assets in production - ruby-on-rails

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

Related

Heroku Public Assets are Stuck

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.

Rails generates wrong asset urls

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.

rails 4 enabled rails to serve static assets: is it correct? (on heroku)

Environment: heroku
Rails: 4
Ruby: 2
We deployed an app to heroku, and it seemed as though anything in the public folder was not accessible (didn't see the static file so the router kicked in and then complains about no route matching).
To get around this, we set
# Rails 4 only flag
config.serve_static_assets = true
in our production.rb file. Is this really the best way to handle this? Or did we fail to configure the app some how to be hosted on heroku properly?
This is deprecated in Rails 4.2, and it is now an alias slated to be removed in Rails 5.0
config.serve_static_assets = true
It should be changed to:
config.serve_static_files = true
In previous Rails versions, Heroku injected a plugin that enabled serving of static assets so this issue didn't exist. As this plugin system was removed in Rails 4, they now created a gem which does the same. You enable it in your Gemfile via:
gem 'rails_12factor', group: :production
See Getting Started with Rails 4.x on Heroku
You could also of course use a CDN for your assets, but you're not required to.
For Rails 5+ work on twelve-factor platforms out of the box and the gem is no longer required
As Dean Winchester mentioned it, it is a good idea to use a CDN for your static assets. In fact when using only Heroku your Rails application would have to be responsible to serve static assets since Heroku Cedar architecture will not do that for you.
Setting config.serve_static_assets = true is the way to go if you don't want to configure a CDN and use only Heroku.
The rails guides are wrong. Try...
config.assets.serve_static_files = true

Most of my assets suddenly return 404 after a 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

Rails compress assets even in development mode

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.

Resources