【问题标题】:assets not compiled error when uploading images in production and staging Carrierwave, fog and S3在生产中上传图像和暂存 Carrierwave、雾和 S3 时资产未编译错误
【发布时间】:2016-03-30 11:00:11
【问题描述】:

由于某种原因,我无法在生产/暂存环境中上传图片,但在开发中一切正常。尝试上传生产中的图片时,我收到了Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError: isn't precompiled

这是(gist)的日志输出

# Carrierwave.rb

require 'fog/aws'

CarrierWave.configure do |config|
    config.fog_credentials = {
        provider:                 'AWS',
        aws_access_key_id:          ENV['AWS_ACCESS_KEY_ID'],
        aws_secret_access_key:  ENV['AWS_SECRET_ACCESS_KEY'],
        region:                                 'us-east-1'
    }
    config.fog_directory = ENV['S3_BUCKET_NAME']
    config.fog_public    = true
    # config.ignore_integrity_errors = false
    # config.ignore_processing_errors = false
end


# Ref:
# https://support.cloud.engineyard.com/entries/20996881-Use-CarrierWave-and-Optionally-Fog-to-Upload-and-Store-Files#update3
# http://stackoverflow.com/questions/7946819/carrierwave-and-amazon-s3

=============================
# AvatarUploader.rb

# encoding: utf-8

class AvatarUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  # storage :file
  storage :fog
  # storage :aws

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    # "tmp/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  process :resize_to_fill => [150, 150]

  version :inner do
    process resize_to_fill: [100, 100]
  end

  version :profile, from_version: :inner do
    process resize_to_fill: [45, 45]
  end

  version :small, from_version: :profile do
    process resize_to_fill: [30, 30]
  end

  version :tiny, form_version: :small do
    process resize_to_fill: [20, 20]
  end

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process :resize_to_fit => [50, 50]
  # end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  def extension_white_list
    %w(jpg jpeg gif png)
  end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "#{expert.email}.jpg" if original_filename
  # end

end

【问题讨论】:

  • 您是否尝试过在生产环境或暂存环境中预编译您的资产。您可以使用RAILS_ENV=production bundle exec rake assets:precompile 命令在生产环境中执行此操作。
  • 你确定这个错误是在你上传的时候出现的,而不是以后你在视图中使用头像的时候吗?
  • @dkp,是的,试过了,但仍然出现同样的错误
  • 我可以上传头像,但是当我提交表单时出现错误。
  • @Thomas R. Koll,当我在提交表单后尝试在视图上显示图片时,如何解决?

标签: ruby-on-rails amazon-s3 carrierwave fog


【解决方案1】:

似乎资产没有预编译。这个问题似乎与 CarrierWave 无关。运行 RAILS_ENV=production bundle exec rake assets:precompile

【讨论】:

  • @Shisir,恐怕这并没有解决问题,但谢谢。
猜你喜欢
  • 1970-01-01
  • 2012-01-21
  • 1970-01-01
  • 1970-01-01
  • 2016-10-14
  • 2012-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多