16

When I run my app locally in test/dev my views come up nicely and everything is happy. When I try to navigate to those same erb files running on my remote server/local production server I get errors like the following:

ActionController::RoutingError (No route matches "/stylesheets/scaffold.css")

I've seen similar questions here on SO but none have been able to solve my problem. The closest thing I've found to an answer is the first answer here: Rails 404 error for Stylesheet or JavaScript files

As I understand it the best thing to do would be to configure my webserver to serve static files. How do I do this locally/on Heroku?

Update

As per raidfive's suggestion I changed config.serve_static_assets from false to true and this fixed my issue. However, I see that it says in production.rb that Apache or nginx should already be serving static assets. Is it any less good/professional to serve static assets in this way and if so how would I achieve the desired results if I'm using Heroku?

Update 2

Apparently Heroku does this automatically, I had an extra comma that was causing the mischief. I was able to look in the extended Heroku logs using the following tip to track down the trouble. Thanks SO!

Community
  • 1
  • 1
keybored
  • 5,194
  • 13
  • 45
  • 70

2 Answers2

33

Are you using Rails 3? By default Rails 3 / webrick does not serve static files in production mode. You can easily enable this by setting

config.serve_static_assets to true in your production.rb file.

zeacuss
  • 2,563
  • 2
  • 28
  • 32
raidfive
  • 6,603
  • 1
  • 35
  • 32
  • Ah ha, I thought it might be something like that. I noticed in the comment above the setting, however, that it says that apache or nginx should already do this. Is there something I should be setting in my server? – keybored Feb 08 '11 at 17:18
  • Ignore that, apparently it's something Heroku does automatically. My issue was the errant placement of a comma that for some reason my local server was ignoring. Thanks for the help! – keybored Feb 08 '11 at 17:59
  • Ah, no problem. Yea, I think Heroku has a doc page about how they handle static assets if you want to read into it. – raidfive Feb 08 '11 at 18:19
  • I modified the `config/environments/production.rb` per the above suggestion to serve static assets but still get a 404 error for a heroku deployed app (landing page or root). – Avid Programmer Jan 22 '21 at 12:57
1

In Rails5, you should comment

"config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?" 

in config/enviroment/production.rb

Rav
  • 1,327
  • 3
  • 18
  • 32
s8186255
  • 181
  • 1
  • 5