Rails 4: assets not loading in production

前端 未结 18 1374
梦如初夏
梦如初夏 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 15:53

    I'm running Ubuntu Server 14.04, Ruby 2.2.1 and Rails 4.2.4 I have followed a deploy turorial from DigitalOcean and everything went well but when I go to the browser and enter the IP address of my VPS my app is loaded but without styles and javascript.

    The app is running with Unicorn and Nginx. To fix this problem I entered my server using SSH with my user 'deployer' and go to my app path which is '/home/deployer/apps/blog' and run the following command:

    RAILS_ENV=production bin/rake assets:precompile
    

    Then I just restart the VPS and that's it! It works for me!

    Hope it could be useful for somebody else!

    0 讨论(0)
  • 2020-11-29 15:54

    The default matcher for compiling files includes application.js, application.css and all non-JS/CSS files (this will include all image assets automatically) from app/assets folders including your gems:

    If you have other manifests or individual stylesheets and JavaScript files to include, you can add them to the precompile array in config/initializers/assets.rb:

    Rails.application.config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']
    

    http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets

    0 讨论(0)
  • 2020-11-29 15:56

    I just had the same problem and found this setting in config/environments/production.rb:

    # Rails 4:
    config.serve_static_assets = false
    
    # Or for Rails 5:
    config.public_file_server.enabled = false
    

    Changing it to true got it working. It seems by default Rails expects you to have configured your front-end webserver to handle requests for files out of the public folder instead of proxying them to the Rails app. Perhaps you've done this for your javascript files but not your CSS stylesheets?

    (See Rails 5 documentation). As noted in comments, with Rails 5 you may just set the RAILS_SERVE_STATIC_FILES environment variable, since the default setting is config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?.

    0 讨论(0)
  • 2020-11-29 15:56

    I may be wrong but those who recommend changing

    config.assets.compile = true

    The comment on this line reads: #Do not fallback to assets pipeline if a precompiled asset is missed.

    This suggests that by setting this to true you are not fixing the problem but rather bypassing it and running the pipeline every time. This must surely kill your performance and defeat the purpose of the pipeline?

    I had this same error and it was due to the application running in a sub folder that rails didn't know about.

    So my css file where in home/subfolder/app/public/.... but rails was looking in home/app/public/...

    try either moving your app out of the subfolder or telling rails that it is in a subfolder.

    0 讨论(0)
  • 2020-11-29 15:58

    In /config/environments/production.rb I had to add this:

    Rails.application.config.assets.precompile += %w( *.js ^[^_]*.css *.css.erb )
    

    The .js was getting precompiled already, but I added it anyway. The .css and .css.erb apparently don't happen automatically. The ^[^_] excludes partials from being compiled -- it's a regexp.

    It's a little frustrating that the docs clearly state that asset pipeline IS enabled by default but doesn't clarify the fact that only applies to javascripts.

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

    Even we faced the same problem where RAILS_ENV=production bundle exec rake assets:precompile succeeded but things did not work as expected.
    We found that unicorn was the main culprit here.

    Same as your case, even we used to restart unicorn after compiling the assets. It was noticed that when unicorn is restarted, only its worker processes are restarted and not the master process.
    This is the main reason the correct assets are not served.

    Later, after compiling assets, we stopped and started unicorn so that the unicorn master process is also restarted and the correct assets were getting served.
    Stopping and starting unicorn brings around 10 secs on downtime when compared to restarting the unicorn. This is the workaround that can be used where as long term solution is move to puma from unicorn.

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