No route matches [GET] /assets
Asked Answered
O

9

151

I have a Rails app that I'm trying to test in the production environment. I ran RAILS_ENV=production rake assets:precompile which generated all of my assets in /public/assets. The problem is that when I start my app w/ RAILS_ENV=production rails s thin I get:

ActionController::RoutingError (No route matches [GET] "/assets/application-eff78fd67423795a7be3aa21512f0bd2.css"):

This file does exist though at /public/assets/application-eff78fd67423795a7be3aa21512f0bd2.css.

Any thoughts as to why I'm getting this RoutingError?

Object answered 19/10, 2011 at 23:33 Comment(0)
F
245

In production mode, Rails will not be responsible for serving static assets. Therefore, you are getting this error. Thin won't do it either, since it's just a wrapper around Rails.

This is controlled by this setting in config/environments/production.rb in your application:

config.serve_static_files = false

Or in Rails 5:

# config/environments/production.rb
config.public_file_server.enabled = true

Or set ENV['RAILS_SERVE_STATIC_FILES'] to true.

You can either set to that true or use a real server like Apache or Nginx which will serve the static assets. I suspect Pow may also do it.


If you're on Heroku, they recommend the use of the rails_12factor gem which enables this setting by default. Place the gem into a production group in your Gemfile, like this:

group :production do
  gem 'rails_12factor'
end
Francinafrancine answered 20/10, 2011 at 0:34 Comment(9)
Anyone know if this a solution to the said problem when deploying to heroku?Mystic
Clear answer, many thanks. I got to this page as I was testing production environment using thin on my development machine. I was compiling the assets but application.css was empty and the server log gave error of the OP.Catechol
In Rails 4 it will be config.serve_static_files. config.serve_static_assets is deprecated and will be removed in Rails 5.Orthogenic
Deploying to a RHEL distribution, I haven't touched the default environment configuration, just added the rails_12factor gem in Rails v4.2.4, now everything is okay. Thanks a lotBdellium
@Onur: This means that your Rails application will be serving the assets, rather than your webserver. I do not recommend this configuration at all because it can make your Rails server slower.Francinafrancine
Default configuration seems to be checking whether an environment variable is present for this option. In this case this sets it to false for me, but from now on I will pay attention and take your advice into your account. That was a nice tip. Thanks a lot again.Bdellium
i am having similar problem cannot download attachment. getting error as ActionController::RoutingError (No route matches "/vol_dev/imported/32790/freshest_data_2016-02-01_2016-02-24.csv". I am using a server and attachment is stored outside rails application. path from root folder.Cammie
I turned 'serve_static...' on. It works on most files, but on one image and some of the JS it does not. Besides the useless rename of the env setting for static_assets, is there some other all-too-clever breakage in the asset system that has been foisted upon us in v4.2 vs v4.0? Maybe I should just put a route in the routes file for every asset that doesn't work with whatever they did?Somerset
On Heroku, rails_12factor gem is not required any more in Rails 6 as the Rails 6 default takes care of it. Reference: <devcenter.heroku.com/articles/…>Impress
B
12

Adding to what Ryan said above, the Rails asset pipeline guide describes how to setup Apache or nginx to serve the static assets for you.

http://guides.rubyonrails.org/asset_pipeline.html

You really should setup nginx or Apache to serve static assets, as they're much better optimized for this task than mongrel/thin/unicorn.

Blameless answered 24/10, 2011 at 16:22 Comment(0)
D
7

Just solved the same problem. In my case Ryan's answer was not helpful. Bratsche pointed to the Rails Guides, unfortunately this didn't work for me too. However the resource was helpful. So I took Nginx configuration from there and added the root directive, pointing to the public directory. Without this it doesn't work.

   # serve static assets
   location ~ ^/assets/ {
     expires 1y;
     root  /path/to/my/cool_project/public;
     add_header Cache-Control public;

     add_header ETag "";
     break;
   }

Restart nginx, and that's it.

Dysphonia answered 13/2, 2013 at 13:4 Comment(0)
R
4

In rails 5, the config.serve_static_files option has changed, so now you need to have

config.public_file_server.enabled = true

to serve assets locally.

Robertson answered 26/12, 2016 at 22:50 Comment(0)
E
3

Indeed you didn't need to modify any default configs. You just recompile assets file again.

remove public/assets

1.rake assets:clobber RAILS_ENV=production

assets compile

2.rake assets:precompile RAILS_ENV=production

3.restart server,eg(nginx)

Effector answered 24/3, 2016 at 13:19 Comment(1)
@SteveO7,obviously,rails use assets pipleline in dev mode by default.Effector
B
2

Rails 4.2 has added/changed this line in your config/environments/ staging.rb and production.rb files:

config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?

If RAILS_SERVE_STATIC_FILES is not set, and you are service assets from your Rails server (like with Unicorn), then it will default to "false", and the RoutingError will occur.

This is an easy fix:

config.serve_static_files = true
Banyan answered 25/1, 2016 at 18:52 Comment(1)
Thanks, this is simplest option. Configuration usually depends on server environment and making it configurable with an env-variable is nice.Binghi
M
1

I use mina+puma+nginx to deploy my Rails 5 application, I got

ActionController::RoutingError (No route matches [GET] "/assets/application-658cf2ab3ac93aa5cb41a762b52cf49d7184509c307922cd3fbb61b237a59c1a.css")

check config/environments/production.rb

# 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?

NGINX already handles this, config it corretcly

upstream puma {
  server unix:///home/deploy/apps/appname/shared/tmp/sockets/appname-puma.sock;
}

server {
  listen 80 default_server deferred;
  # server_name example.com;

  root /home/deploy/apps/appname/current/public;
  access_log /home/deploy/apps/appname/current/log/nginx.access.log;
  error_log /home/deploy/apps/appname/current/log/nginx.error.log info;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 10M;
  keepalive_timeout 10;
}

things will work fine.

Maeve answered 21/7, 2017 at 8:15 Comment(0)
T
1

try below code:

config/environments/production.rb

config.assets.compile = true

then run command:

RAILS_ENV=production rake assets:precompile

then push all compiles files and manifest file to server.

Tasteful answered 21/7, 2017 at 8:18 Comment(0)
R
1

If somebody get here with the same error in the test environment as I do, here's what helped me:

rails assets:clobber assets:precompile RAILS_ENV=test

then:

ps axu | grep your-username

to find spring server process and his PID then kill it via:

kill <spring-server-PID>
Resht answered 21/2, 2018 at 16:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.