CarrierWave default_url不触发 [英] CarrierWave default_url not triggering

查看:68
本文介绍了CarrierWave default_url不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难让它开始工作。现在,我在查看页面上使用image_tag帮助器。如果没有加载图片,我需要它使用Carrierwave的后备功能,而不仅仅是抛出错误。

I'm having a hard time getting this to work. Right now I'm using the image_tag helper on my view pages. If there isn't an image loaded, I need it to use the fallback from Carrierwave instead of just throwing an error.

对于我的设置,我正在使用雾进行存储,我觉得那可能是问题的一部分。

For my setup I'm using fog for storage, I feel like that might be part of the problem.

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

Carrierwave是否找到未设置的图像?还是我应该采用某种特殊的方法来获取图片网址以使其正常工作?这就是我现在所拥有的。

Does Carrierwave just find any image that isn't set? Or is there some special way I'm supposed to grab the image url for it to work? This is what I have now.

<%= 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

Property.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


推荐答案

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

您在Rails控制台中,并且拥有带有图像的资产。虽然 Asset.first.image_url 看起来像返回一个url(假设您使用的是远程存储),但实际上不是字符串,但它是一个上载器。 Asset.first.image_url.class 将返回 ImageUploader 。要获取网址,您需要添加调用上传器的 url 方法。

Let's assume you are in rails console and you have an asset with an image. While Asset.first.image_url looks like its returning something like a url (assuming you are using remote storage), but its actually not a string, it's an uploader. Asset.first.image_url.class will return ImageUploader. To get the url, you need to add call the url method of the uploader.

这篇关于CarrierWave default_url不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆