2

Why does image_path insist on appending assets/ to the front of image paths that are stored in public/images?

I'm building a photographer's website, so naturally we want photos to be in it, including a random sub-selection of some photos to be shown as part of the layout. Further, I've built a super-simple gallery that just displays all photos. Those image files are stored in:

public/images/gallery

I've gotten an ImageMagick gem to shrink them to the desired size. Those small files are stored in:

public/images/gallery/sm/

The shrinking is done on page load as part of rendering the layout ERB. I know it might initially sound awful, but it only shrinks an image once, and the lazy-shrinking means we don't have to restart the server to add new photos (which he'd want to do).

My reading suggests that the asset pipeline is for static layout stuff, but this is more dynamic than that. I'm led to believe that public/images is where this stuff should go, especially since production-mode Rails complains that the generated thumbnails are not compiled.

Enter the problem: I place those images in the paths shown above, and image_path gives back what looks like an asset pipeline path. It doesn't even work when I flatten the subdirectories and have everything live in public/images.

My workaround is to build the tag manually. So is image_path (and by extension, image_tag) only for asset pipeline stuff? Am I supposed to construct tags from strings for public images? I've also found mentions that image_path is supposed to look in public for a matching file first, but I've also seen documentation (http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html) claiming that it won't verify the existence of something it returns a path for.

Seth
  • 528
  • 3
  • 16
  • 32

1 Answers1

0

Edit: error in original answer

Rails asset pipeline is for serving assets that are present when the application starts. For serving dynamic assets as you suggest there's this answer to a similar question: Rails 3.1 assets not recognizing new images uploaded by rmagick until server restart which answers far better than my answer just did.

Having searched around to improve on my first answer, I think the best solution to the image_tag creation then is to create your own image_tag-like helper. Perhaps:

def public_image_path(filename)
  [your path to that file]
end

def public_image_tag(filename, options={})
  image_tag(public_image_path(filename), options)
end
Community
  • 1
  • 1
Richard Jordan
  • 8,066
  • 3
  • 39
  • 45
  • This is basically what I ended up doing. As an addendum, part of that method needs to include I'm adding a root slash at the beginning of the path to end up with a string like `"/images/gallery/bob.jpg"`. That root slash is important to stop image_tag from thinking its an asset. Edit: That said, it's still perplexing why this doesn't work by default. – Robin Miller Feb 16 '13 at 17:51
  • I think - and as you can tell by my score I'm no deep-knowledge expert - it's because the asset pipeline is compiled on startup, so your dynamic images wouldn't be available at that time. – Richard Jordan Feb 16 '13 at 19:47
  • The assets are compiled to the `public` directory anyways. Since the docs claim it doesn't verify existence of the resource prior to generating the path, I suspect that it just always assumes its an asset unless given that root slash. I'd be okay with that as intended use, but only if it were documented. It is not, unfortunately. – Robin Miller Feb 23 '13 at 23:37