Carrierwave 上传程序上传文件,但仍提供 default_url
我在 Rails 3 应用程序中使用 Carrierwave 从远程源上传文件并将其存储在我的服务器文件系统中。我有一个完全标准的设置,上传器安装在与图像关联的模型上。
它在 99.9% 的时间里都能完美运行,但每隔 600 个图表左右我就会遇到一个问题,即应用程序始终无法提供存储的图像。如果我检查文件系统,图形图像已上传并存储在正确的位置,具有正确的文件权限和所有内容,但 Rails 完全不知道这一点,并继续为该实例提供默认图形图像。
换句话说,id 为 123 的图表的图像存储在 /uploads/graphs/123/graph.png ...正确的图像在那里,但就 Rails 而言,它没有存储图像。所有其他图形图像仍然工作正常,但我无法让 Rails 知道为 123 存储的图像。删除并重新存储图像不起作用。手动删除图像并重新上传不起作用。我完全迷路了。图形实例有效,保存时日志中没有错误。
例如,在控制台中:
g = Goal.find_by_id("123")
g.remote_graph_url = "http://image.source/url.png"
> "http://image.source/url.png"
g.save
> true
g.graph?
> false
g.graph_url >> /default/image.png
这是相关代码:
class GraphUploader < CarrierWave::Uploader::Base
def store_dir
"../uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def default_url
"/images/" + [version_name, "default_large_graph.png"].compact.join('_')
end
def extension_white_list
%w(jpg jpeg gif png)
end
def filename
"graph.png" if original_filename
end
end
class Graph
mount_uploader :graph, GraphUploader
end
I'm using carrierwave in a rails 3 application to upload and store a file from a remote source in my server's file system. I've got a setup that's totally standard, with an uploader mounted on the model that the image is associated with.
It works perfectly 99.9% of the time, but every 600th graph or so I run into an issue where the app persistently fails to serve the stored image. If I check on the filesystem, the graph image has been uploaded and stored in the correct location, with the correct file permissions and everything, but rails is totally unaware of it and continues to serve the default graph image for that instance.
In other words a graph with id 123 has it's image stored at /uploads/graphs/123/graph.png ... the correct image is there but as far as rails is concerned it has no image stored. All the other graph images still work fine, but I can't get rails aware of the image stored for 123. Removing and re-storing the image doesn't work. Manually removing the image and re-uploading doesn't work. I'm totally lost. The graph instance is valid, no errors in the logs when I save.
for example, in the console:
g = Goal.find_by_id("123")
g.remote_graph_url = "http://image.source/url.png"
> "http://image.source/url.png"
g.save
> true
g.graph?
> false
g.graph_url >> /default/image.png
here's the relevant code:
class GraphUploader < CarrierWave::Uploader::Base
def store_dir
"../uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def default_url
"/images/" + [version_name, "default_large_graph.png"].compact.join('_')
end
def extension_white_list
%w(jpg jpeg gif png)
end
def filename
"graph.png" if original_filename
end
end
class Graph
mount_uploader :graph, GraphUploader
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
希望我能回复...
那么您正在将上传内容保存在公共目录之外吗?如果这不是您的意图,请尝试删除图像前的“../”。
Wish I could just reply...
So you are saving uploads outside of the public directory? Try removing the "../" before images, if that wasn't your intention.