rails - application.css asset not found in production mode

前端 未结 4 1711
夕颜
夕颜 2021-01-31 16:38

I\'m upgrading an application to use the asset pipeline.

I\'ve got the css assets compiling into an application css file but they not being found when I run the applicat

相关标签:
4条回答
  • 2021-01-31 16:48

    The Rails 5 solution is similar to the Rails 4 solution given by Jules Copeland above.

    In your pre-generated config/environments/production.rb file, there should be an entry that looks something like this:

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

    I found a decent explanation for this setting in the Configuring Rails Applications guide at http://guides.rubyonrails.org:

    config.public_file_server.enabled configures Rails to serve static files from the public directory. This option defaults to true, but in the production environment it is set to false because the server software (e.g. NGINX or Apache) used to run the application should serve static files instead. If you are running or testing your app in production mode using WEBrick (it is not recommended to use WEBrick in production) set the option to true. Otherwise, you won't be able to use page caching and request for files that exist under the public directory.

    Conclusion: In production, starting your rails server with RAILS_SERVE_STATIC_FILES=1 will allow Rails to serve any files in the public/assets directory just as a web server would. Keep in mind, Rails is an app server and will not do this as efficiently as a web server (e.g. NGINX, Apache, etc.). For real-world applications, you should have a dedicated web server sitting in front of Rails which will serve static assets by itself and only bother Rails for dynamic content as needed. For more details, see this article by Justin Weiss on the differences between web servers and app servers.

    0 讨论(0)
  • 2021-01-31 16:55

    When you do rake assets:precompile, your assets go into public directory. See if you can find those files in public/assets/

    You should see something like this:

    I, [2014-02-23T20:06:21.853314 #26915]  INFO -- : Writing app_root/public/assets/application-ecd8636fc80ea2b712039c4abc365da9.css
    
    0 讨论(0)
  • 2021-01-31 17:03

    Rails by default doesn't serve assets under public. See your production.rb:

      config.serve_static_assets = true
    

    Change that to true and you're good to go. (Note: you don't want that to be true in production, remember to change it back before deploying!)

    See Configuring Rails Applications for details.

    In rails 6, in the default production.rb there should be a line

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

    So run your server with

    RAILS_SERVE_STATIC_FILES=true rails server -e production
    

    or set config.public_file_server.enabled=true in production.rb. See answers below for rails 4 and 5.

    0 讨论(0)
  • 2021-01-31 17:07

    In Rails 4, you can get them to show in production (running locally), by passing an environment variable:

    RAILS_SERVE_STATIC_FILES=true rails server -e production

    This should work as long as you have this line in /config/environments/production.rb:

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

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