Roman
Roman

Reputation: 10403

Why does Rails give 404 error for all assets?

I've deployed my rails app with the Asset Pipeline enabled like so inside the application.rb file:

# Enable the asset pipeline
config.assets.enabled = true

When I checking, I can see the files have been processed by the compiler inside the public/assets directory but when I browser the website none of the assets can be resolved by the browser. If I directly access the assets I will see a 404 message by Nginx.

Can someone tell me what's going on?

Upvotes: 1

Views: 1316

Answers (3)

wael34218
wael34218

Reputation: 4930

Basically the files that are compiled and gzipped are not present in the right directory.

Try setting config.assets.compile = true in config/environments/development.rb if you are running on a development environment. It will generate the asset file on the fly when it is called. This is bad for production since it will deteriorate the performance

So if you are on a production environment try running

rake assets:precompile

Upvotes: 3

Roman
Roman

Reputation: 10403

Even though the answers above were good that wasn't my problem. Rails was doing the write thing except Nginx wasn't configured properly. My Nginx config was pointing to the main application directory instead of the public directory.

So it was originally:

server {
  server_name wptrends.envato.com;
  root /home/deployer/apps/report/current;
  ...

when it should have been this:

server {
  server_name wptrends.envato.com;
  root /home/deployer/apps/report/current/public;
  ...

Upvotes: 0

iltempo
iltempo

Reputation: 16012

I stumbled upon the same problem recently. I'm assuming you are talking about the production environment. The solution was to require sprockets again in the application.rb either by doing

require 'sprockets/railtie'

or

require 'rails/all'

This has been lost because we did not use the whole Rails framework and turned off ActiveRecord in favor of Mongoid. The pitfall is that when updating Rails to newer versions you may not notice that rails/all is changing it's contents.

Upvotes: 1

Related Questions