【问题标题】:Rails 4 - Heroku deploy works but images in CSS file do not appearRails 4 - Heroku 部署工作,但 CSS 文件中的图像不出现
【发布时间】:2014-08-27 09:43:54
【问题描述】:

我尝试了多种方法来显示一些图像,这些图像在 Heroku 部署时嵌入到我的 style.css.erb 文件中。我对部署时在本地显示的图像没有任何问题,所以我假设这是由于预编译的资产。这是我所拥有的,非常感谢任何帮助/建议:

style.css.erb

#parallax1{ 
  background-image:url('<%= asset_path("parallax/1.jpg")%>');
}

#parallax2{ 
  background-image:url('<%= asset_path("parallax/2.jpg")%>');
}

#intro{
position: relative; 
margin: 0 auto;
width: 100%;
overflow-x: hidden;
height:650px;
z-index:2;
background-image: url('<%= asset_path("bgslides/3.jpg")%>');
background-size:cover;
}

生产.rb

config.assets.precompile += %w( jquery.js bootstrap.min.js jquery.nav.js jquery.scrollTo.js grid.js custom.js stellar.js modernizr.custom.js jquery.simple-text-rotator.min.js wow.min.js mb.bgndGallery.effects.js mb.bgndGallery.js font-awesome.css.scss.erb animate.css bootstrap.min.css simpletextrotator.css style.css.erb *.jpg *.png )

assets.rb

config.assets.precompile += %w( jquery.js bootstrap.min.js jquery.nav.js jquery.scrollTo.js grid.js custom.js stellar.js modernizr.custom.js jquery.simple-text-rotator.min.js wow.min.js mb.bgndGallery.effects.js mb.bgndGallery.js font-awesome.css.scss.erb animate.css bootstrap.min.css simpletextrotator.css style.css.erb *.jpg *.png )

应用程序.rb

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

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module DryerPlug
 class Application < Rails::Application
   config.assets.paths << Rails.root.join("app", "assets", "fonts")
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.

# Set Time.zone default to the specified zone and make Active Record auto-convert to this        zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
 end
end

宝石文件

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.1'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer',  platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more:                
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0',          group: :doc

gem 'spring',        group: :development
gem 'mail_form'
gem 'simple_form'
gem 'pg', group: :production
gem 'rails_12factor', group: :production

【问题讨论】:

  • 检查已部署应用程序的 CSS。它试图使用什么路径来查找图像?
  • 它试图在 heroku_app_name.herokuapp.com/assets/bgslides/1.jpg 中找到它们。它完全缺少图像文件夹。不知道这是否能告诉你什么......
  • 您是否尝试过手动操作heroku run rake assets:precompile?总是值得一试。
  • 是的,这似乎成功了。它正在寻找资产路径,但不是看起来的指纹版本。
  • 看看这里:https://devcenter.heroku.com/articles/rails-4-asset-pipeline#dependencies-improperly-used。不过在此之前,您可能想尝试做一个bundle update,因为最近有something related changed in sprockets。

标签: css ruby-on-rails heroku ruby-on-rails-4 erb


【解决方案1】:

您是否尝试在本地编译资产?这通常对我有用。

首先清理当前资产

rake assets:clean

然后再次预编译它们

rake assets:precompile

然后提交这些资产并推送到 Heroku。如果这不起作用,请尝试通过在每个命令中添加 RAILS_ENV=production 以在生产模式下运行。

【讨论】:

    【解决方案2】:

    SCSS

    您遇到的问题是您在 CSS 中调用了 ruby​​-on-rails erb 方法 - 这虽然可行,但可能会导致许多问题

    解决此问题的更好方法是使用 Rails CSS preprocessors 之一,以便您的应用程序可以正确预编译资产。典型的 CSS 预处理器是SCSS:

    #app/assets/stylesheets/application.css.scss
    #parallax1{ 
      background-image: asset_url("parallax/1.jpg");
    }
    

    --

    预编译

    你需要保持你的样式表 100% CSS(或 SCSS)——当你 precompile 他们(部署到 Heroku 所需)时,Rails 将无法使用 erb 的大部分逻辑/功能文件将创建

    虽然您在使用erb 功能时是正确的,但最好的方法是使用SCSS 预处理器。最重要的是,您需要确保已在本地预编译资产:

    $ rake assets:precompile RAILS_ENV=production
    $ git add .
    $ git commit -a -m "Precompiled"
    $ git push heroku master
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 2016-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-19
      相关资源
      最近更新 更多