Rails App Not Serving Assets in Production Environment

前端 未结 8 1967
佛祖请我去吃肉
佛祖请我去吃肉 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 01:47

    Check for a file like this:

    public/assets/.sprockets-manifest-3f7771d777ceb581d754e4fad88aa69c.json
    

    If you are pushing precompiled assets to a production server there is a chance that you are preventing hidden 'dot' files being pushed and this essential file won't make it into production.

    In my environment I need to precompile assets in an integration environment and push these to production so that there is no need to compile the assets on the production machine. I was erroneously blocking all hidden files from being pushed to production machine.

    To see if this answer works for you, check your generated HTML source in a browser from the production server to see if the assets path has been generated. If you see your script tag like this:

    <script data-turbolinks-track="true" src="/javascripts/application.js"></script>
    

    check the src attribute. It should start with /assets/javascript. In this case it starts with /javascript which indicates Rails does not think any of the assets have been precompiled.

    I corrected this by updating my push to production (currently rsync), ensuring I push the .sprockets-manifest* file after precompiling on my integration server.

    Also, I use standalone Passenger as my integration test server, rather than Webrick, since it handles more realistic serving of static files.

    0 讨论(0)
  • 2020-12-01 01:48

    This sounds like to the problem I was having.

    I found a blog that suggests this is a bug in the Rails 4.0.0 asset pipeline, and is inexplicably mitigated by setting...

    config.assets.compile = true
    

    ... in config/environments/production.rb

    Aside from somehow kicking the asset pipeline in to actually working, that setting will turn on live-compilation of assets. That is normally a bad thing for performance in production, but if you still manually precompile assets when you deploy, with

    rake assets:precompile
    

    ... the live-compilation should never happen (because the necessary assets have already been precompiled).

    I hope this helps :)

    0 讨论(0)
  • 2020-12-01 01:49

    In production.rb change the setting:

    rails 3.x

    config.serve_static_assets = true
    

    rails 4.x

    config.serve_static_files = true
    
    0 讨论(0)
  • 2020-12-01 01:52

    The following command works for me locally.

    rails server -e production
    

    I got the same error "ActionController::RoutingError (No route matches [GET] "/assets/application.css"" while running "rails s". Even after I precompiled the source, change config precompile true. It still could not load properly.

    The option "-e production" made those RoutingError disappear.

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

    As previously noted config.serve_static_assets is deprecated and replaced by config.serve_static_files. If one examines config/environments/production.rb for Rails-4.2 then one finds this:

      # Disable serving static files from the `/public` folder by default since
      # Apache or NGINX already handles this.
      config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
    

    The implication being that setting and exporting (in BASH) the environment variable export RAILS_SERVE_STATIC_FILES="to any value whatsoever" in a session prior to running rails s -e production will give the desired result when testing locally and also will avoid having to remember to recode production.rb before pushing to the production host.

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

    in Rails 5.x the setting is

    config/initializers/assets.rb:

    Rails.application.config.public_file_server.enabled = true
    

    https://edgeguides.rubyonrails.org/configuring.html

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