Rails App Not Serving Assets in Production Environment

前端 未结 8 1968
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 01:29

My app works fine when run in development environment. In production (rails server -e production), the browser can\'t access the css and js files and on the con

相关标签:
8条回答
  • 2020-12-01 02:05

    I think to Rails 4.x you have to precompile assets to production or use config.assets.compile even both if needed.

    The default Rails behavior for production environment is to "Do not fallback to assets pipeline if a precompiled asset is missed." So, don't. Use to not compi

    config.assets.compile = false
    

    If you use this option you don't need to use:

    config.serve_static_files = true
    

    Because if the asset wasn't precompiled, Rails will compile before serve request.

    But if you do precompile the assets before production you don't needs config.assets.compile = true, but you need config.serve_static_files = true to Rails serve requests if you don't have http_server to serve the precompiled assets.

    The setting config.serve_static_assets is deprecated.

    DEPRECATION WARNING: The configuration option `config.serve_static_assets` has been renamed to `config.serve_static_files` to clarify its role (it merely enables serving everything in the `public` folder and is unrelated to the asset pipeline). The `serve_static_assets` alias will be removed in Rails 5.0. Please migrate your configuration files accordingly.
    

    I hope this answer help you(reader) to understand whats really happens

    0 讨论(0)
  • 2020-12-01 02:06

    When testing locally your production environment, you have to compile the assets locally. Simply run the command below:

    RAILS_ENV=production bundle exec rake assets:precompile
    

    It will generate all the assets under public/assets.

    Next, you have to tell Rails to serve the assets itself. Server software (eg. Nginx or Apache) do it for you on environments like Heroku, but locally you should let Rails do it. Change this in your production.rb:

    config.serve_static_assets = true
    

    But make sure you set it back to false before pushing your code to production!

    0 讨论(0)
提交回复
热议问题