简体   繁体   中英

No images and fonts production using Capistrano and Rails

In my local server all works fine, but in the production (EC2 Amazon) some, but not all, of my fonts and images don't load.

This is application.rb:

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

# Pick the frameworks you want:
require "active_model/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
require 'carrierwave'
require 'carrierwave/orm/activerecord'

# require "rails/test_unit/railtie"

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

module TestApp
  class Application < Rails::Application
    #adding fonts to assets pipeline
    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.
    config.autoload_paths << Rails.root.join('lib')
    # 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 = :ru
    config.serve_static_files = true
    config.serve_static_assets = true
    config.assets.compile = true
  end
end

A lot of times assets will load in development, but once in production depending on how you are linking to the assets, the links can break. Make sure you are using the asset link helpers provided by Rails to link to assets.

Examples:

In regular views:

 <%= image_tag "rails.png" %>

You can use sass-helpers for referencing fonts and images in stylesheets like:

 src: font-url(...)

 image-url("rails.png") /** becomes url(/assets/rails.png) **/
 image-path("rails.png") /** becomes "/assets/rails.png".  **/

If you aren't using SASS you can change your css into css.erb and do things like:

 .class { background-image: url(<%= asset_path 'image.png' %>) }

More Asset link examples: http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM