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

Posted

技术标签:

【中文标题】Ruby on rails bootsy + cloudinary 上传图片麻烦【英文标题】:Ruby on rails bootsy + cloudinary upload image trouble 【发布时间】:2018-02-05 07:18:18 【问题描述】:

我有 RoR 项目,生活在 heroku 上。我有 bootsy(具有图像上传功能的编辑器),我有 cloudinary。 我已经设置了上传器、cloudinary api 密钥和初始化程序(如果需要,可以向您展示)。现在,当我尝试在 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) 模型/article.rb

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

  mount_uploader :main_image, ArticleImageUploader
  mount_uploader :list_image, ArticleImageUploader

end

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

【问题讨论】:

请添加任何错误消息和日志输出,否则我们无法诊断问题。 @Зелёный 我已经展示了屏幕。没有错误消息。只是一个空的img。在 Rails 日志中 - 一切看起来也不错。在 cloudinary 图像中创建。在数据库中创建行 如果你知道的话,还有 webbrowser 控制台。 @Зелёный 没有错误,控制台也是空的。 根本没有魔法,你漏掉了一些简单的东西,我确定是js的问题。 【参考方案1】:

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

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

像这样在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..

【讨论】:

您好!我完全重写了这个 gem,我现在将尝试检查你的解决方案,如果它完美运行 - 我将再次添加赏金 嘿,它运行良好,谢谢!但它根本不接受尺寸 - 就像我可以选择中号或小号一样 - 它正在加载相同的图像 @Legendary,我想,如果你仔细看 bootsy gem 的源代码,你会发现它可能从来没有正常工作过,例如:github.com/volmer/bootsy/blob/master/app/assets/javascripts/… but image url of various versions生成如github.com/cloudinary/cloudinary_gem#try-it-right-away 中所述

以上是关于Ruby on rails bootsy + cloudinary 上传图片麻烦的主要内容,如果未能解决你的问题,请参考以下文章

模型文件中的 Ruby on Rails IBAN 验证

思考Ruby On Rails的底层代码(Ruby on Rails 開發秘籍 | Ruby on Rails 快速入門)

ruby on rails如何安装

ruby on rails - 问题捆绑安装nokogiri 1.7.2 on ruby on rails 4.x.

Ruby on Rails入门篇

markdown [rails:devise] Ruby on Rails的身份验证gem。 #ruby #rails