gpt4 book ai didi

ruby-on-rails - Ruby on Rails bootsy + cloudinary 上传图片麻烦

转载 作者:行者123 更新时间:2023-12-03 00:39:25 32 4
gpt4 key购买 nike

我有 RoR 项目,生活在 heroku 上。我有 bootsy (具有图像上传功能的编辑器),我有 cloudinary。我已经设置了 uploader 、cloudinary api key 和初始化程序(如果需要,可以向您展示)。现在,当我尝试在 bootsy 中上传图像时 - 它会创建数据库行,并在 cloudinary 中创建图像。但是在 bootsy 的 js 窗口中,有空 <img>

ruby '2.3.1'
gem 'rails', '~> 5.1.1'
gem 'bootsy'
gem 'carrierwave'
gem 'fog'
gem 'cloudinary', '~> 1.8.1'

1) uploaders/bootsy/image_uploader.rb

module Bootsy
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick

# storage Bootsy.storage

include Cloudinary::CarrierWave

def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

version :large do
process :eager => true
process resize_to_fit: [
700, 700
]

end

version :medium do
process :eager => true
process resize_to_fit: [
300, 300
]
end

version :small do
process :eager => true
process resize_to_fit: [
150, 150
]
end

version :thumb do
process :eager => true
process resize_to_fit: [
150, 150
]
end


def extension_white_list
%w(jpg jpeg gif png)
end
end
end

2) 初始化器/bootsy.rb

Bootsy.setup do |config|
config.image_versions_available = [:small, :medium, :large, :original]
config.storage = :fog
end

3) models/article.rb

class Article < ApplicationRecord
include Bootsy::Container
mount_uploader :image, Bootsy::ImageUploader

mount_uploader :main_image, ArticleImageUploader
mount_uploader :list_image, ArticleImageUploader

end

This what I've in browser And html code from inspect

P.S 好吧,我真的不知道 - 我只是在公共(public)存储库中重复这个错误。 https://bitbucket.org/dekakisalove/bootsy_tes/我会尽快为这个问题添加悬赏。

最佳答案

此问题是由于方法 store! 的返回值不正确造成的类Cloudinary::CarrierWave::Storage

要解决此问题,您可以使用多种变体,例如:

像这样 config/initializers/cloudinary_store.rb

module CloudinaryStorage
def store!(file)
super || uploader.metadata
end
end

ActiveSupport.on_load :after_initialize do
Cloudinary::CarrierWave::Storage.prepend CloudinaryStorage
end

或者像 app/uploaders/image_uploader.rb 中这样

module Bootsy
class ImageUploader < CarrierWave::Uploader::Base
after :store, :reload_data

def reload_data(file)
model.reload
end
# etc..

关于ruby-on-rails - Ruby on Rails bootsy + cloudinary 上传图片麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45911900/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com