5

I am trying to make a MissingImage class like this:

class MissingImage

  include ActionView::Helpers::AssetUrlHelper

  def src format
    return asset_path('layout/missing_image.png')
  end
end

To use like this in the model:

def main_image
  images.find(&:ismain) || images.first || MissingImage.new
end

The problem is - asset_path when used like this simply returns the string without any connection to the asset pipeline.

Suggestions?

UPDATE

After pocking around I have refactored MissingImage somewhat, but it still doesn't generate signed asset path in production:

class MissingImage
  def src format
    return ActionController::Base.helpers.asset_path('layout/missing_image.png')
  end
end

The path returned is /assets/layout/missing_image.png

firedev
  • 20,898
  • 20
  • 64
  • 94

1 Answers1

0

For my purposes '/assets/icons/myimage.png' was good enough but it sounds like you want the fully qualified path. I think if you try to get an absolute url you might have better luck.

You could set the option on the config.action_controller.asset_host and then use:

#{asset_path("layout/missing_image.png")}

check out the comments in the accepted answer on this question - Rails 3.1 absolute URL to an image

Community
  • 1
  • 1
spartikus
  • 2,852
  • 4
  • 33
  • 38