Kevin Sylvestre Kevin Sylvestre - 5 months ago 29
Ruby Question

Dragonfly and Short URLs

I'm using Dragonfly in a project that returns a large stream of photos and was looking to optimize the URLs. I'm currently getting image URLs like:


Which is over 256 bytes. I'd like something like:


That is conforming to:


How would I go about adding this when using Dragonfly and Rails such that
maps to a chain of operations and
is used to find the model or image? Thanks!


I've added custom
for each of the formats I need and have the following working:

# config/routes.rb
match "/photos/:id/:style", to: "photos#show", as: :media

# app/controllers/photos_controller.rb
def show
@photo = Photo.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.jpg { cache('public', 86400); redirect_to @photo.url(params[:style], 'jpg') }
format.png { cache('public', 86400); redirect_to @photo.url(params[:style], 'png') }
format.gif { cache('public', 86400); redirect_to @photo.url(params[:style], 'gif') }

# app/views/photos/show.html.erb
<%= image_tag media_path(id:, style: 'small', format: 'png') %>

However this results in a
for each image (but otherwise works fine). Is it possible to handle this as a render or somehow do an internal redirect (i.e. not require the client to make duplicate requests)?


you shouldn't need to use a controller action - you can use a dragonfly endpoint - see


match '/photos/:id/:style.:format' => Dragonfly[:images].endpoint { |params, app|

or something like that (haven't tried the above code but it'll be something along those lines)