为什么我无法在 Carrierwave 中编辑此图像?

发布于 2024-12-09 11:34:35 字数 459 浏览 5 评论 0原文

我有两个 Mongoid 模型:Store 和 Product。他们的关系是一个Store has_many Products,并且Products owns_to Store。每个模型都有一些可以使用 Carrierwave 附加的图像,如下所示:

mount_uploader :logo, ImageUploader

我可以添加和编辑 Store 模型中的图像。但在产品中,我只能在创建产品时添加图像,而不能在编辑产品时添加图像。这似乎是一个 deep_copy 问题,类似于在 Mongoid 中如果你有一个名为 urls 的数组并且想要更新该数组,则必须调用

urls_will_change!

所以我尝试在 before_update 回调中调用等效方法(logo_will_change!),但它没有做任何事情。我应该在其他地方这样做还是这是另一个问题?

I have two Mongoid models, Store and Product. Their relationship is that a Store has_many Products, and Products belongs_to Store. Each of these models has some images that can be attached using Carrierwave, that look like this:

mount_uploader :logo, ImageUploader

I am able to add and edit images that are in the Store model. But in Product I can only add an image on creation off a Product, but not with editing a product. This seems somehow to be a deep_copy issue, similar to how in Mongoid if you have an array called urls and you want to update that array, you must call

urls_will_change!

So I have tried calling the equivalent method (logo_will_change!) inside of a before_update callback, but it doesn't do anything. Is there somewhere else I should be doing this or is it another problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

提笔书几行 2024-12-16 11:34:35

下面的代码对我有用,所以可能还有其他事情发生:

# store model
class Store
  include Mongoid::Document
  mount_uploader :image, ImageUploader
  has_many :products
  field :name, type: String
end

# product model
class Product
  include Mongoid::Document
  mount_uploader :image, ImageUploader
  belongs_to :store
  field :name, type: String  
end

# image uploader
class ImageUploader < CarrierWave::Uploader::Base
  storage :file
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

# some test data
@store = Store.new({:name => "store"})
@product = Product.new({:name => "product"})
@store.save
@store.products << @product

# later get the product and update the image
@product = Product.first
puts @product.image.url # blank
@product.update_attributes({:image => File.open("/path/to/image.png")})
puts @product.image.url # now has image url

The code below worked for me, so there may be something else going on:

# store model
class Store
  include Mongoid::Document
  mount_uploader :image, ImageUploader
  has_many :products
  field :name, type: String
end

# product model
class Product
  include Mongoid::Document
  mount_uploader :image, ImageUploader
  belongs_to :store
  field :name, type: String  
end

# image uploader
class ImageUploader < CarrierWave::Uploader::Base
  storage :file
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

# some test data
@store = Store.new({:name => "store"})
@product = Product.new({:name => "product"})
@store.save
@store.products << @product

# later get the product and update the image
@product = Product.first
puts @product.image.url # blank
@product.update_attributes({:image => File.open("/path/to/image.png")})
puts @product.image.url # now has image url
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文