上传到dropbox的图片在一段时间后仍未显示 - Rails

时间:2015-11-05 14:52:13

标签: ruby-on-rails ruby-on-rails-4 ckeditor dropbox carrierwave

我正在使用ckeditor,我将图片上传到我使用carrierwaveminimagick配置的Dropbox。几天后,虽然图片没有出现在博客文章中,但它在控制台中显示以下内容

Failed to load resource: the server responded with a status of 401 (Not Authorized)

示例链接(资源图像)

https://dl.dropboxusercontent.com/1/view/5qo62va4g4uwsqn/uploads/ckeditor/pictures/1/content_1.png

如果我转到ckeditor并再次浏览图像并重新加载它,它会工作几天然后相同。

有什么我想念的吗?

1 个答案:

答案 0 :(得分:1)

由于carrierwave-dropbox gem使用API​​的media部分和greg,如评论中所指出的,此链接会在几个小时后过期。

这是我使用shares

使其工作的程序

首先找到carrierwave-dropbox gem安装位置导航到

lib\carrierwave\storage\dropbox.rb

并将行54更改为以下

@client.shares(@path, short_url = false)["url"]

接下来在application_helper.rb

中制作一个帮助方法
  def rewrite_url(url)
    u = url
    u.slice! "dl=0"
    u = u + "raw=1"
    u
  end

基本上所做的就是从返回的字符串中删除dl = 0部分,因为它指向在dropbox上预览并在末尾添加raw=1以实际显示直接文件。

接下来,在图片标记

上调用它

<%= image_tag rewrite_url(@question.image_url(:resized)) %>

:resized是因为我调整了图像的大小,关键部分是将图片网址包装到帮助器中

read_more:more info

非常感谢 Greg 的帮助。