0

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

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
6
  • What error are you seeing? Can you confirm that you access the fallback image (put localhost:3000/assests/fallback.default.jpg into your browser)
    – cgat
    Mar 6, 2013 at 5:52
  • I'm getting a no record error because the image_tag is looking for an image url in the db where there is none. Typing the route to the image in the browser window just gave me a routing error.
    – humdinger
    Mar 7, 2013 at 1:40
  • If you are seeing a no record error, that means you don't have any assets in your property. For the default_url to work, you need to create an asset without an image. btw, I spelt assets wrong above, so you should be trying 'localhost:3000/assets/fallback.default.jpg'
    – cgat
    Mar 7, 2013 at 1:53
  • So let me see if I understand this. It doesn't use a default url when there is no image, it just defaults when there is a URL but not image to match it? I tried with a few different url variations in the URL and still got nothing
    – humdinger
    Mar 7, 2013 at 5:30
  • I'm not sure if we are on the same page, but I'll try to explain it a bit further. Let's say you have an Email model. That model has an attachment, which is one of many attributes of Email. Let's say you mount the uploader on attachment. If you create an email record, with no attachment (none uploaded), then Email.first.attachment_url will give you the default_url. If you don't have an email in the first place, then can't get the default url. In your case, the Asset is the Email, and image is the attachment. Maybe if you could give more code your problem would be clearer.
    – cgat
    Mar 7, 2013 at 6:17

1 Answer 1

0

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

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.

1
  • This didn't work. Any other ideas? <%= image_tag(property.assets.first!.image_url.url, :width => "200") %>
    – humdinger
    Mar 9, 2013 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.