我在Rails 5.2.2.1上使用Carrierwave gem。当数据库中不存在该记录时,我将其设置为图像的默认URL。但是,如果我在数据库中有一个图像用于记录,我怎么能显示它,但该文件在磁盘上不存在?
目前,我收到以下错误:
ActionController :: RoutingError(没有路由匹配[GET]“/uploads/player/314c38a9-6567-4faa-bfda-9e7a75cbaed4.jpeg”)
有没有办法拯救并显示我在上传器文件中指定的默认网址?
如果您有默认图像,则可以像这样在模型中设置方法
def image
File.file?(file.path) ? file.url : default_image_url #and you need to define this default url
end
或者您可以在路线中提到的@mroach处理图像上的404