Rails 4: assets not loading in production

前端 未结 18 1375
梦如初夏
梦如初夏 2020-11-29 15:15

I\'m trying to put my app into production and image and css asset paths aren\'t working.

Here\'s what I\'m currently doing:

  • Image assets live in /app/a
相关标签:
18条回答
  • 2020-11-29 16:01

    change your Production.rb file line

    config.assets.compile = false
    

    into

    config.assets.compile = true
    

    and also add

    config.assets.precompile =  ['*.js', '*.css', '*.css.erb']
    
    0 讨论(0)
  • 2020-11-29 16:09

    There are 2 things you must accomplish to serve the assets in production:

    1. Precompile the assets.
    2. Serve the assets on the server to browser.

    1) In order to precompile the assets, you have several choices.

    • You can run rake assets:precompile on your local machine, commit it to source code control (git), then run the deployment program, for example capistrano. This is not a good way to commit precompiled assets to SCM.

    • You can write a rake task that run RAILS_ENV=production rake assets:precompile on the target servers each time you deploy your Rails app to production, before you restart the server.

    Code in a task for capistrano will look similar to this:

    on roles(:app) do
      if DEPLOY_ENV == 'production'
        execute("cd #{DEPLOY_TO_DIR}/current && RAILS_ENV=production rvm #{ruby_string} do rake assets:precompile")
      end
    end
    

    2) Now, you have the assets on production servers, you need to serve them to browser.

    Again, you have several choices.

    • Turn on Rails static file serving in config/environments/production.rb

      config.serve_static_assets = true # old
      
      or
      
      config.serve_static_files = true # new
      

      Using Rails to serve static files will kill your Rails app performance.

    • Configure nginx (or Apache) to serve static files.

      For example, my nginx that was configured to work with Puma looks like this:

      location ~ ^/(assets|images|fonts)/(.*)$ {
          alias /var/www/foster_care/current/public/$1/$2;
          gzip on;
          expires max;
          add_header Cache-Control public;
      }
      
    0 讨论(0)
  • 2020-11-29 16:09

    If precompile is set you DO NOT need

    config.assets.compile = true
    

    as this is to serve assets live.

    Our problem was we only had development secret key base set in config/secrets.yml

    development:
        secret_key_base: '83d141eeb181032f4070ae7b1b27d9ff'
    

    Need entry for production environment

    0 讨论(0)
  • 2020-11-29 16:11

    What you SHOULD NOT do:

    Some of my colleagues above have recommended you to do this:

    config.serve_static_assets = true  ## DON”T DO THIS!! 
    config.public_file_server.enabled = true ## DON”T DO THIS!!
    

    The rails asset pipeline says of the above approach:

    This mode uses more memory, performs more poorly than the default and is not recommended. See here: (http://edgeguides.rubyonrails.org/asset_pipeline.html#live-compilation)

    What you SHOULD do:

    Precompile your assets.

    RAILS_ENV=production rake assets:precompile

    You can probably do that with a rake task.

    0 讨论(0)
  • 2020-11-29 16:12

    Found this:

    The configuration option config.serve_static_assets has been renamed to config.serve_static_files to clarify its role.

    in config/environments/production.rb:

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

    So set env RAILS_SERVE_STATIC_FILES or using Nginx to serving static files. Add config.serve_static_assets = true will still work, but removed in future.

    0 讨论(0)
  • 2020-11-29 16:13

    For Rails 5, you should enable the follow config code:

    config.public_file_server.enabled = true

    By default, Rails 5 ships with this line of config:

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

    Hence, you will need to set the environment variable RAILS_SERVE_STATIC_FILES to true.

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