3

Carrierwave アップローダーでデフォルトの画像を設定する際に問題があります。URL の前に奇妙なクラスを追加しているようですが、画像をレンダリングしていないようです。以下の私のコードを参照してください。

ヘルパー

 # encoding: utf-8

class UserpicUploader < CarrierWave::Uploader::Base

  CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/
  # Include RMagick or MiniMagick support:
  include CarrierWave::RMagick
  # include CarrierWave::MiniMagick
  # Include the Sprokets helpers for Rails 3.1+ asset pipeline compatibility:
  # include Sprockets::Helpers::RailsHelper
  # include Sprockets::Helpers::IsolatedHelper

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

  # 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}"
  end



  # Process files as they are uploaded:

  version :normal do
   process :resize_to_fill => [162, 163]
  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:
       # asset_path("fallback/" + [normal, "profile_default_pic.png"].compact.join('_'))
       "/images/fallback/" + [normal, "profile_default_pic.png"].compact.join('_')
     end

  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process :scale => [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 
      if original_filename 
        @name ||= Digest::MD5.hexdigest(File.dirname(current_path))
        "#{@name}.#{file.extension}"
      end
    end

end

編集...

これが機能した編集済みのコードです。Assets/images/fallback ではなく public/fallback に画像を配置する必要がありました。

# encoding: utf-8

class UserpicUploader < CarrierWave::Uploader::Base

  CarrierWave::SanitizedFile.sanitize_regexp = /[^[:word:]\.\-\+]/
  # Include RMagick or MiniMagick support:
  include CarrierWave::RMagick
  # include CarrierWave::MiniMagick
  # Include the Sprokets helpers for Rails 3.1+ asset pipeline compatibility:
  include Sprockets::Helpers::RailsHelper
  include Sprockets::Helpers::IsolatedHelper

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

  # 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}"
  end



  # Process files as they are uploaded:

  version :normal do
   process :resize_to_fill => [162, 163]
  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:
         asset_path("fallback/" + [normal, "profile_default_pic.png"].compact.join('_'))
       # "/fallback/" + [normal, "profile_default_pic.png"].compact.join('_')
     end

  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process :scale => [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 
      if original_filename 
        @name ||= Digest::MD5.hexdigest(File.dirname(current_path))
        "#{@name}.#{file.extension}"
      end
    end

end
4

3 に答える 3

3

わかりました、これは私が見逃していたものです...

  • デフォルトのフォルダーは、assets/images ではなく public/ フォルダーにある必要がありました
  • 次の行をアップローダー クラスに追加する必要がありました。

    インクルード Sprockets::Helpers::RailsHelper

    インクルード Sprockets::Helpers::IsolatedHelper

  • default_url を次のように変更する必要がありました。

    asset_path("fallback/" + [通常, "profile_default_pic.png"].compact.join('_'))

于 2012-04-27T01:49:14.183 に答える
0

Rails 4.1.*では、 default_urlメソッドでファイル名を指定するだけです。

def default_url
  "default.png"
end

この場合、default.png/app/assets/imagesフォルダーにある必要があります

于 2015-01-21T05:42:06.607 に答える
0

アップローダ クラスを編集します。

def default_url  
 "/images/fallback/" + [version_name,"default.png"].compact.join('_')
end

次に、デフォルトの画像をコピーします/images/fallback/default.png

于 2013-04-07T14:44:19.133 に答える