Assuming that your attachment model is called "Asset" add the following line to "config/routes.rb":
map.asset_data '/asset/:id', :controller => 'assets', :action => 'show'
Create an AssetController containing at least the following:
(The thumbnail of the original image is found if necessary, and mimetype_fu is used to determine the mime-type sent to the browser)
class AssetsController < ApplicationControllerAdd this method to app/helpers/application_helper.rb
def show
asset = Asset.find(params[:id])
if params[:thumbnail]
data = asset.thumbnails.find_by_thumbnail(params[:thumbnail])
end
data ||= asset
send_data (data.db_file.data,
:filename => data.filename,
:type => File.mime_type?(data.filename),
:disposition => 'inline')
end
end
def assetpath(asset,thumb=nil)
"#{asset_data_url(:id => asset.id)}#{thumb.nil? ? '' : "?thumbnail=#{thumb.to_s}"}"
end
So now you may use the helper in your views (i.e.):
<%= image_tag(assetpath(asset_model_instance)) %>
<%= image_tag(assetpath(asset_model_instance,:thumb)) %>
No comments:
Post a Comment