0

これを機能させるのに苦労しています。現在、ビュー ページで image_tag ヘルパーを使用しています。画像が読み込まれていない場合は、エラーをスローするだけでなく、Carrierwave からのフォールバックを使用する必要があります。

私のセットアップでは、ストレージにフォグを使用していますが、それが問題の一部であると感じています。

def default_url
  asset_path("fallback/default.jpg")
end

Carrierwave は、設定されていない画像を見つけるだけですか? または、画像のURLを取得して機能させる特別な方法はありますか? これは私が今持っているものです。

<%= image_tag(property.assets.first!.image_url, :width => "200") %>

Asset.rb

require 'file_size_validator'
class Asset < ActiveRecord::Base
  validates :image_url, uniqueness: true
  validates :image_url, allow_blank: true, format: {
      with: %r{\.(gif|jpg|png)$}i,
      message: 'must be a URL for GIF, JPG or PNG image.'
  },
            :file_size => {
                :maximum => 0.5.megabytes.to_i
            }

  attr_accessible :image_url, :property_id
  belongs_to :property
  mount_uploader :image_url, ImageUploader

end

プロパティ.rb

  validates :street_address, :city, :state, :description, :price, :deposit, :beds, :baths, :presence => true
  validates :street_address, :uniqueness => true
  validates :price, :deposit, numericality: {greater_than_or_equal_to: 0.01}

  has_many :assets, :dependent => :destroy

  attr_accessible :street_address, :street_address2, :city, :state, :zip, :country, :description, :price, :beds, :baths,
                  :deposit, :availability, :leased, :sqft, :pets
  attr_accessible :assets_attributes
  accepts_nested_attributes_for :assets, :reject_if => lambda { |p| p[:image_url].blank? }, :allow_destroy => true
4

1 に答える 1

0

試す<%= image_tag(property.assets.first!.image_url.url, :width => "200") %>

Railsコンソールを使用していて、画像付きのアセットがあると仮定します。URLのようなものを返すようにAsset.first.image_url見えますが(リモートストレージを使用していると仮定)、実際には文字列ではありませんが、アップローダーです。Asset.first.image_url.classを返しImageUploaderます。urlURLを取得するには、アップローダーのメソッドの呼び出しを追加する必要があります。

于 2013-03-08T07:42:30.070 に答える