Updated CSS Stylesheet not loaded following deployment to Heroku?

偶尔善良 提交于 2019-12-29 07:16:10

问题


This has been a problem for me for a while now, but I still can't figure out how the asset pipeline works in Rails 4. I finally learned how to pre-compile assets, but once again after deployment, my CSS Stylesheet isn't getting updated.

I confirmed this by going to Developer Tools and viewing the source. It looks different from my CSS files. My guess is the problem lies in my production.rb file.

Production.rb

Games::Application.configure do

  config.cache_classes = true
  config.eager_load = true
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true
  config.serve_static_assets = true
  config.assets.js_compressor = :uglifier
  config.assets.compile = true
  config.assets.digest = true
  config.assets.version = '1.0'
  config.log_level = :info
  config.i18n.fallbacks = true
  config.active_support.deprecation = :notify
  config.log_formatter = ::Logger::Formatter.new
end

Application.rb

require File.expand_path('../boot', __FILE__)

require 'rails/all'

Bundler.require(*Rails.groups)

module Games
  class Application < Rails::Application
    config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)
    config.exceptions_app = self.routes
  end
end

Here is my Application.html.erb file with the helpers.

<!DOCTYPE html>
  <html>

    <head>
      <title><%= @title %></title>
      <%= stylesheet_link_tag    "application", media: "all",
      "data-turbolinks-track" => true %>
      <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
      <%= csrf_meta_tags %>
    </head>

    <body>

      <%= yield %>

      <%= render 'layouts/footer' %>

    </body>

  </html>

Gem File

gem 'rails', '4.0.4'

group :development, :test do
  gem 'sqlite3', '1.3.8'
  gem 'rspec-rails', '2.13.1'
end

group :production do
  gem 'pg', '0.17.1'
  gem 'rails_12factor'
end

group :test do
  gem 'selenium-webdriver', '2.35.1'
  gem 'capybara', '2.1.0'
end

gem 'sass-rails', '~> 4.0.2'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'sprockets-rails', '~> 2.0.0'
gem 'bootstrap-sass', '2.3.2.0'

gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.2'

Here are the steps I've taken

heroku run rake assets:precompile RAILS_ENV=production
git add .
git commit
git push heroku master

Now, maybe I'm mistaken, but having run the git add . (meaning add all files) it should have loaded the latest stylesheet as well. But once again, it seems like Heroku failed.

This has happened before, and is getting annoying, so I would like to find an explanation for this.

Thank you for your time.

Edit:

I think I now know what the problem is. My stylesheets never get updated to my public/assets folder. I don't know what I can do to make them appear there.


回答1:


It looks like you have added assets precompiled file in your git repo. Ideally, it shouldn't be there since heroku can do this for you whenever you push it.

To fix this, you have to do this

  1. git rm -r public/assets/
  2. add public/assets/** in your .gitignore file
  3. git add .
  4. git commit -am "allow heroku auto assets precompilation"
  5. git push heroku master



回答2:


Okay, let me explain how this should work for you


Precompilation

Precompiling your assets is going to give you "different" files / stylesheets.

This is because the precompilation process appends an MD5 hash to the filenames of your asset files - defining them for the production environment. This is called "asset fingerprinting":

Fingerprinting is a technique that makes the name of a file dependent on the contents of the file. When the file contents change, the filename is also changed. For content that is static or infrequently changed, this provides an easy way to tell whether two versions of a file are identical, even across different servers or deployment dates.

Essentially, precompilation is a way for Rails to minify your asset files, for production. Why is this important? Several reasons - efficiency, file size and concatenation:

In the production environment Sprockets uses the fingerprinting scheme outlined above. By default Rails assumes assets have been precompiled and will be served as static assets by your web server.


Static Assets

When you upload to Heroku, and run in any other production environment, Rails will expect your assets to be pre-compiled.

Precompilation expects your assets to be located in the public/assets folder - allowing your application to call them when it's ready. The problem with this is that if your assets are not precompiled, it will likely result in some errors

You've not mentioned what the specific problem is that you have. There are a number of issues to content:

  1. You need to reference your assets using the asset path helpers
  2. You need to ensure you've called your assets in your Layout etc
  3. You need to make sure you have the correct files

Here's the bottom line of what you need to do:

$ rake assets:precompile RAILS_ENV=production

This will precompile your assets locally

You then need to ensure you have the assets referenced correctly in your layout:

#app/views/layouts/application.html.erb
<%= stylesheet_link_tag "application" %>

This will give you the ability to call the precompiled assets throughout your Rails app. This does not cover having syntax issues, which you may have preventing the precompilation process from proceeding




回答3:


follow this

  1. rake assets:precompile
  2. git add .
  3. git commit -am "your commit message"
  4. git push heroku master

This is suppose to work.




回答4:


If your using sass...before you go through all this other trouble...make sure your not mixing .css and .scss files in your assets folder.
Heroku seems to have some trouble mixing the two when compiling assets. I can't explain why this is or if my statement is true...but, in my own experience all I had to do to fix this was simply rename any .css files to .scss. And, the next push...everything loaded correctly.




回答5:


Something else to consider if you're reading these solutions without success.

What prevented my css/scss files updating on production was that i'd neglected to add .sass-cache to .gitignore, so the folder had been submitted to revision control and thus overriding changes on production.



来源:https://stackoverflow.com/questions/25455384/updated-css-stylesheet-not-loaded-following-deployment-to-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!