Kev*_*tre 3 ruby ruby-on-rails dragonfly-gem
我在一个项目中使用Dragonfly,该项目返回大量照片并希望优化URL.我目前正在获取图片网址:
哪个超过256个字节.我喜欢这样的东西:
HTTP://本地主机:3000 /媒体/ 1024/240x240_medium.jpg
这符合:
/media/:id/:format
Run Code Online (Sandbox Code Playgroud)
在使用Dragonfly和Rails时,我如何添加它,以便:format映射到一系列操作并:id用于查找模型或图像?谢谢!
编辑:
我Mime::Type为我需要的每种格式添加了自定义,并具有以下工作:
# 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') }
end
end
# app/views/photos/show.html.erb
<%= image_tag media_path(id: @photo.id, style: 'small', format: 'png') %>
Run Code Online (Sandbox Code Playgroud)
然而,这导致302每个图像(但在其他情况下工作正常).是否可以将其作为渲染处理或以某种方式进行内部重定向(即不要求客户端发出重复请求)?
您不需要使用控制器操作 - 您可以使用蜻蜓端点 - 请参阅http://markevans.github.com/dragonfly/file.URLs.html#Routed_Endpoints
例如
match '/photos/:id/:style.:format' => Dragonfly[:images].endpoint { |params, app|
Photo.find(params[:id]).image.thumb(params[:style]).encode(params[:format])
}
Run Code Online (Sandbox Code Playgroud)
或类似的东西(没有尝试上面的代码,但它将是这些行的东西)