I'm using a controller in my Rails 4 app to serve some images, to which I want to restrict access (similar to this answer). The images that are being served are not stored in the asset pipeline themselves, and are programmatically generated. If the image hasn't been generated yet, I want to show a default / fallback image to the user. This image is stored with other static assets.
So how can I get the actual path to this image file to give to send_file
in my controller? As noted here the asset_path and image_path
helpers do not give the local filesystem path, but because of the asset pipeline fingerprinting stuff, I can't just construct the path myself either.
class ThingsController
before_filter :authorize_for_thing
def show
respond_to do |format|
format.all {
if @thing.graph.file.present?
file = @thing.graph_url()
else
### SERVE THE STATIC DEFAULT IMAGE IF THE FILE ISN'T THERE: ###
file = File.join(Rails.root,
ActionController::Base.helpers.asset_path("default_graph.png")
)
end
send_file file, :type=>"image/png", :disposition => 'inline'
}
end
end
end
But asset_path("default_graph.png") give something of the form:
assets/default_graph[possible-md5].png
When the file actually lives elsewhere, and send_file needs the literal path to the file.