回形针不显示图像(rails api)

问题描述 投票:0回答:2

我有一个用户模块,我已生成回形针附件:profile_pic

user.rb:

has_attached_file :profile_pic,
style: { :medium => "300x300>", thumb: "100x100>" },
default_url: "/images/:style/missing.png"

控制器:

image_base = params[:manager][:profile_pic]
if image_base != nil
   image = Paperclip.io_adapters.for(image_base)
   image.original_filename = params[:manager][:file_name]
   current_user.profile_pic = image
   current_user.errors.delete(:profile_pic)
   current_user.save 
end

配置/初始化/ paperclip.rb:

Paperclip::DataUriAdapter.register

它没有显示任何错误,但如果我试图显示图像,它会给我以下错误:

ActionController::RoutingError (No route matches [GET] "/system/managers/profile_pics/000/000/008/original/icon_new.png"):

当我在控制台中尝试时:

user.profile_pic.display
/system/managers/profile_pics/000/000/008/original/icon_new.png? 
1556082410 => nil 

图片保存在公共文件夹中

ruby-on-rails-5 paperclip
2个回答
0
投票

默认文件夹如下,因此Paperclip上传到正确的位置::rails_root/public/system/:class/:attachment/:id_partition/:style/:filename

但看起来您在访问正确的位置时遇到了麻烦。你试过user.profile_pic.url吗?我尝试了我的工作示例,.display.url都是相同的,但我使用的是AWS S3存储桶。

user.profile_pic.url应该在网上找到该文件的位置。

user.profile_pic.path应该在文件系统上找到文件的位置。


0
投票

问题在于生产模式,并通过在production.rb中添加以下行来解决此问题

config.public_file_server.enabled = true
© www.soinside.com 2019 - 2024. All rights reserved.