使用image_tag方法在公共目录()中提供图像非常简单,但是对于我不希望公开访问的图像,可能是仅供管理员使用的图像。
我的控制器和视图应该如何看起来,以便image_tag方法能够服务于来自root/private/images而不是root/public的图像。而且,那是完全安全的,对吗?如果我有私有映像,并且只想为管理员提供服务,那么使用一个简单的If语句来检查用户是否是管理员,然后使用image_tag为他们服务,那么来自公用文件夹之外的私有映像是否是安全的?
我想知道如何在没有回形针的情况下完成这个。(连回形针都没有提到,所以不是复制品)
发布于 2014-02-10 04:39:09
假设图像与一个模型相关,比如一个附有回形针的模型,你可以这样做。(我真的会推荐这个)。
def show
@image = Image.find(params[:id])
published_image = @image.published_image
respond_to do |format|
format.all {
if published_image
send_file published_image.attached_image.path, :type=> published_image.attached_image_content_type
else
raise ActionController::RoutingError.new('Not Found')
end
}
end
end假设您有另一种方法来说明这一点,您可以让所有路由以/admin/image路由开始到控制器操作,然后在路径的其余部分调用呈现文件。示例:
routes.rb
get 'admin/photo/*other', to: 'admin#photos'
admin.controller.rb
def photos
if current_user.admin?
render file: Rails.root.join("private","admin","images",request.path.gsub("/admin/photo/","")
else
render :file => "public/401.html", :status => :unauthorized
end
end
view
image_tag "/admin/photos/id.jpg"理论上这是可行的,但可能比我想的要复杂得多
这里的图像将托管在公共/admin/映像中,但链接到as /admin/照片/id
发布于 2014-02-10 04:24:51
我不知道您是如何为管理员处理身份验证的,但前提是您有单独的登录。
send_data返回请求的映像文件这个链接有一个例子
https://stackoverflow.com/questions/21668976
复制相似问题