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:16

    In rails 4 you need to make the changes below:

    config.assets.compile = true
    config.assets.precompile =  ['*.js', '*.css', '*.css.erb'] 
    

    This works with me. use following command to pre-compile assets

    RAILS_ENV=production bundle exec rake assets:precompile
    

    Best of luck!

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

    I was able to solve this problem by changing: config.assets.compile = false to
    config.assets.compile = true in /config/environments/production.rb

    Update (June 24, 2018): This method creates a security vulnerability if the version of Sprockets you're using is less than 2.12.5, 3.7.2, or 4.0.0.beta8

    0 讨论(0)
  • 2020-11-29 16:17
    location ~ ^/assets/ {
      expires 1y;
      add_header Cache-Control public;
      add_header ETag "";
    }
    

    This fixed the problem for me in production. Put it into the nginx config.

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

    Rails 4 no longer generates the non fingerprinted version of the asset: stylesheets/style.css will not be generated for you.

    If you use stylesheet_link_tag then the correct link to your stylesheet will be generated

    In addition styles.css should be in config.assets.precompile which is the list of things that are precompiled

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

    First of all check your assets, it might be possible there is some error in pre-compiling of assets.

    To pre-compile assets in production ENV run this command:

    RAILS_ENV=production rake assets:precompile
    

    If it shows error, remove that first,

    In case of "undefined variable" error, load that variable file before using it in another file.

    example:

    @import "variables";
    @import "style";
    

    in application.rb file set sequence of pre-compiliation of assets

    example:

    config.assets.precompile += [ 'application.js', 'admin.js', 'admin/events.js', 'admin/gallery.js', 'frontendgallery.js']
    
    config.assets.precompile += [ 'application.css', 'admin.css','admin/events.css', 'admin/gallery.css', 'frontendgallery.css']
    
    0 讨论(0)
  • 2020-11-29 16:19

    it is not recommended to let capistrano do assets precompile, because it may take ages and often time out. try to do local assets precompile.

    1st, set in config/application.rb config.assets.initialize_on_precompile = false then do local RAILS_ENV=production bin/rake assets:precompile and add those public/assets to git.

    and config/environments/development.rb, change your asset path to avoid using precompiled assets:

    config.assets.prefix = '/dev-assets'

    If you have db connection issue, means u have initializer that uses db. one way around it is to set a new environment by duplicate production.rb as maybe production2.rb, and in database.yml, add production2 environment with development db setting. then do

    RAILS_ENV=production2 bin/rake assets:precompile

    if you are still facing some issue with assets, for example ckeditor, add the js file into config/initializers/assets.rb

    Rails.application.config.assets.precompile += %w( ckeditor.js )

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